In a numbered ordering of the document's nodes, insertBefore() will place newNode at the index held by refNode, and increment refNode's index and all subsequent nodes' indices. This is instead of maintaining refNode's index, placing newNode at refNode's position minus one, and shifting all previous nodes' indices down by one.
The base case of refNode.index = 0 demonstrates why this must be the case, but it is good to know this explicitly, as it affects methods that deal with iteration such as getElementsByTagName().
DomNode->insert_before
(No version information available, might be only in CVS)
DomNode->insert_before — 新規ノードを子ノードとして挿入する
説明
この関数は新規ノード newnode をノード refnode の直前に挿入します。 戻り値は挿入されたノードです。 もし追加された子ノードを変更するつもりであれば、 返されたノードを使用する必要があります。
(PHP >= 4.3 のみ) newnode がすでに文章の一部である場合、最初に既存のコンテキストから削除されます。 もし refnode が NULL の場合、 newnode は子ノードリストの最後に挿入されます。
domnode_insert_before() は domnode_append_child() に非常に似ており、 以下の例は domnode_append_child() にある例と同様のことを行うことを示しています。
例1 子ノードを追加する
<?php
include("example.inc");
if (!$dom = domxml_open_mem($xmlstr)) {
echo "Error while parsing the document\n";
exit;
}
$elements = $dom->get_elements_by_tagname("informaltable");
print_r($elements);
$element = $elements[0];
$newnode = $element->insert_before($element, $element);
$children = $newnode->children();
$attr = $children[1]->set_attribute("align", "left");
echo "<pre>";
$xmlfile = $dom->dump_mem();
echo htmlentities($xmlfile);
echo "</pre>";
?>
domnode_append_child() も参照ください。
DomNode->insert_before
captainbajoo at juno dot com
12-May-2006 12:40
12-May-2006 12:40
