我有一个像这样格式化的 XML
<things>
<thing>
<name>foo</name>
<id>1</id>
</thing>
<thing>
<name>bar</name>
<id>2</id>
</thing>
</things>
我创建了一个新的 <thing>
带有表单信息的元素。当我这样做时$dom->appendChild($newthing)
,它被附加到文档末尾的 </things>.
之后像这样
<things>
<thing>
<name>foo</name>
<id>1</id>
</thing>
<thing>
<name>bar</name>
<id>2</id>
</thing>
</things>
<thing>
<name>bar</name>
<id>2</id>
</thing>
显然我希望我的新元素成为根元素的子元素,而不是在它之后。我怎么做?
最佳答案
$dom->documentElement->appendChild($newthing);
来自PHP manual :
documentElement
This is a convenience attribute that allows direct access to the child node that is the document element of the document.
关于PHP DOMDocument appendChild 到根目录末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863429/