是否可以使用xpath/xquery查询特定的xml节点,然后向其导入/添加子节点?
示例(代码取自http://codepad.org/gJ1Y2LjM http://codepad.org/gJ1Y2LjM,这是在类似的问题中提出的,但不相同):
1.
我想添加一个数组
$book = array('isbn'=>123456789099, 'title' => 'Harry Potter 3', 'author' => 'J K. Rowling', 'edition' => '2007');
2.
到现有的 XML。
$doc = new DOMDocument();
$doc->loadXML('<?xml version="1.0"?>
<books>
<book>
<isbn>123456789098</isbn>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<edition>2005</edition>
</book>
<book>
<placeItHere></placeItHere>
<isbn>1</isbn>
<title>stuffs</title>
<author>DA</author>
<edition>2014</edition>
</book>
</books>');
3.
为此,使用以下片段。
$fragment = $doc->createDocumentFragment();
$fragment->appendXML(" <book>
<isbn>{$book['isbn']}</isbn>
<title>{$book['title']}</title>
<author>{$book['author']}</author>
<edition>{$book['edition']}</edition>
</book>
");
4.
但我没有将其附加到根节点,而是几乎在互联网上找到了每个示例:
$doc->documentElement->appendChild($fragment);
5.
我想将它(p.ex)附加到 /books/book/placeItHere 中找到的节点,而不是使用 getElementbyId 或 tagName,而是使用 xpath/xquery。我尝试过
$xp = new domxpath($doc);
$parent = $xp->query("books/book/placeItHere");
到达该节点,但从未设法将其用作父节点。
问题:如何使用该位置来appendChild $fragment?是否可以?
**YOUR BEAUTIFUL MAGIC**
最后我只会保存它。
echo $doc->saveXML();
感谢您能给我的任何帮助。