助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
片段 ``` DOMDocumentFragment extends DOMNode { /* 方法 */ //追加原始XML数据 public appendXML ( string $data ) : bool } ``` >[danger]将DOMDocumentFragment添加到另一个节点时,它有点特殊。 发生这种情况时,不会将片段本身添加为子代,而是将片段的所有子代移到新的父节点上。 ``` /* Create a document and a fragment containing a single node */ $doc = new DOMDocument(); $fragment = $doc->createDocumentFragment(); $fragment->appendChild($doc->createElement('foo')); /* Now, the foo node is a child of the fragment */ var_dump($fragment->firstChild); /* After appending the fragment to another node, the children of the * fragment will have been transfered to that node (and the fragment is * not present in the children list!) */ $doc->appendChild($fragment); /* So the fragment has no children anymore */ var_dump($fragment->firstChild); /* But $doc has a single child, which is the foo element, not the * fragment */ var_dump($doc->childNodes->length); var_dump($doc->firstChild); ?> This produces the following output: object(DOMElement)#3 (0) { } NULL int(1) object(DOMElement)#3 (0) { } ```