PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DomNode->append_sibling> <DomNode->add_namespace
Last updated: Fri, 10 Oct 2008

view this page in

DomNode->append_child

(No version information available, might be only in CVS)

DomNode->append_child 子ノードの最後に新規に子ノードを追加する

説明

DOMNode append_child ( DOMNode $newnode )

この関数は既存の子ノードに子ノードを追加する、 もしくは新規子ノードを作成します。

パラメータ

newnode

追加するノード。これには、例えば DomDocument->create_element, DomDocument->create_text_node など、もしくは単純にあらゆる他のノードによって作成されたノードを 指定することが可能です。

注意: このメソッドを使用して DOMAttribute を追加することはできません。代わりに DomElement->set_attribute を使用してください。

返り値

成功時は追加されたノード、失敗時は FALSE を返します。

変更履歴

バージョン 説明
4.3.0 他の文章からのノードを挿入することは、もはや許可されません。
4.3.0 PHP 4.3.0 以前では、新規子ノードは追加する前に複製されます。 そのため、新規子ノードは完全に新しいコピーとなります。これは、 この関数に渡されたノードを変更することなしに修正することが可能です。 渡されたノードが子ノード自身の場合うまくコピーされますので、 XML 文章の大きな部位を簡単にコピーすることができます。 戻り値は追加された子ノードです。 もし追加された子ノードを変更するつもりであれば、 返されたノードを使用する必要があります。
4.3.0 と 4.3.1 新規子ノード newnode がすでに DomNode の子ノードである場合、最初に既存のコンテキストから削除されます。 そのため、newnode は移動され、 コピーされません。この動作は W3C 規格に準じるものです。 もし古い動作をさせる必要がある場合、追加ではなく DomNode->clone_node を使用してください。
4.3.2 新規子ノード newnode がすでにツリーにある場合、 最初に既存のコンテキストから削除されます。同じ規則が適用されます。

以下の例は新規ドキュメントに新規要素ノードを追加し、 属性 alignleft に設定します。

例1 子ノードを追加する

<?php
$doc 
domxml_new_doc("1.0");
$node $doc->create_element("para");
$newnode $doc->append_child($node);
$newnode->set_attribute("align""left");
?>

上記の例は、以下のようにも書くことができました:

例2 子ノードを追加する

<?php
$doc 
domxml_new_doc("1.0");
$node $doc->create_element("para");
$node->set_attribute("align""left");
$newnode $doc->append_child($node);
?>

より複雑な例は以下の通りです。最初に特定の要素を検索し、 子を含むノードを複製し、兄弟ノードとして追加します。 最後に新規属性を新規兄弟ノード能古ノードの一つに追加し、 文章全体をダンプします。

例3 子ノードを追加する

<?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];

$parent $element->parent_node();
$newnode $parent->append_child($element);
$children $newnode->children();
$attr $children[1]->set_attribute("align""left");

$xmlfile $dom->dump_mem();
echo 
htmlentities($xmlfile);
?>

上記の例は DomNode->append_child の代わりに DomNode->insert_before を用いても動作します。

PHP 5 への移行

DOMNode::appendChild を使用してください。



add a note add a note User Contributed Notes
DomNode->append_child
andrew at transitionmedia dot co dot uk
12-Feb-2006 04:03
As of version 4.3 PHP doesn't support Appending a child from another source document. If you are trying to  import information from multiple sources into a final document [for transformation using XSL as an example] then you can have problems. This technique can be used to do it though.

I am assuming you have two documents open, $xmldoc1 and $xmldoc2 and you have selected [via XPath or explicit searching] the nodes you want in each document. Thus $xmldoc1_appending_node is the node you would like to add $xmldoc2_importnode to.

<?

// first we create a temporary node within the document we want to add to.
$temp_importnode = $xmldoc1->create_element("import");

// now we have a node that is in the right document context we can clone the one we want into this one.
$temp_importnode = $xmldoc_importnode->clone_node(true);

// by using true in the above call to clone_node() we copy all of the child nodes as well. Use false or nothing if you just want the containing node with no children.

$xmldoc1_appending->append_child($importnode);

?>

Now your document contains the new nodes imported from a different document.
wackysalut at bourget dot cc
01-Nov-2002 09:46
Be careful of the order you append childs. (PHP <= 4.2)

If you create let's say:

$x_html = $x_doc->create_element('html');
$x_head, $x_title, $x_meta, $x_link and $x_body in the same way.

If you:

$x_head->append_child($x_title);
$x_head->append_child($x_meta);
$x_head->append_child($x_link);

$x_html->append_child($x_head);

$x_body_backup = $x_html->append_child($x_body);

/* here */

$x_doc->append_child($x_html);

If you now try to modify $x_body_backup.. it will no longer be able to modify the actualy XML tree, because you appended the HTML tree to the DomDocument.. and it has copied ALL the nodes at the end of the DomDocument, so the reference you now have in $x_body_backup is not valid anymore! If you had modified it where the /* here */ line is.. the $x_body_backup would still be valid.. but not after the $x_doc->append_child($x_html);

What you can do is, reverse the order of your appendings.. so that you append the highest level node at the end.. so that your reference is still valid after, or wait till you have filled all of the nodes you wanted to add before you append it to your parent node.

DomNode->append_sibling> <DomNode->add_namespace
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites