Php 需要将特定的XML节点从一个文档复制到另一个文档
我有两个XML文件(有许多公共节点),看起来有点像:Php 需要将特定的XML节点从一个文档复制到另一个文档,php,xml,xpath,Php,Xml,Xpath,我有两个XML文件(有许多公共节点),看起来有点像: DESTINATION FILE: ('destination.xml') <items> <item> <title>Item A</title> <description>This is the description for Item A</description> <id>1001</
DESTINATION FILE: ('destination.xml')
<items>
<item>
<title>Item A</title>
<description>This is the description for Item A</description>
<id>1001</id>
</item>
<item>
<title>Item B</title>
<description>This is the description for Item B</description>
<id>1002</id>
</item>
<item>
<title>Item D</title>
<description>This is the description for Item D</description>
<id>1004</id>
</item>
目标文件:('DESTINATION.xml')
项目A
这是项目A的说明
1001
B项
这是项目B的说明
1002
项目D
这是项目D的说明
1004
及
源文件:('SOURCE.xml')
项目A
这是项目A的说明
1001
项目C
这是项目C的说明
1003
B项
这是项目B的说明
1002
我需要从源抓取“id”匹配“1003”(在本例中)的节点,并将其导入目标。我希望了解如何使用importNode(或simpleXML选项),以及如何使用xpath只获取所需的节点 要获得正确的节点,我认为XPath应该如下所示:
$xpath->query('/items/item/id[.="1003"]/..')
要将其导入其他文档,您需要创建文档并调用importNode
,第二个参数设置为true
:
$newDom = new DOMDocument;
$newDom->load('destination.xml');
$newNode = $newDom->importNode($el, true);
$newDom->firstChild->appendChild($newNode);
file_put_contents('destination.xml', $newDom->saveXML());
只要这样做,它就会起作用:
<?php
header('Content-type: application/xml'); //Just to test in the browser directly and have a good format
$docSource = new DOMDocument();
$docSource->loadXML(file_get_contents('source.xml'));
$docDest = new DOMDocument();
$docDest->loadXML(file_get_contents('destination.xml'));
$xpath = new DOMXPath($docSource);
$result = $xpath->query('//item[id=1003]')->item(0); //Get directly the node you want
$result = $docDest->importNode($result, true); //Copy the node to the other document
$items = $docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result); //Add the copied node to the destination document
echo $docDest->saveXML();
loadXML(文件获取内容('source.xml');
$docDest=新的DOMDocument();
$docDest->loadXML(文件获取内容('destination.xml');
$xpath=newdomxpath($docSource);
$result=$xpath->query('//item[id=1003]')->item(0)//直接获取所需的节点
$result=$docDest->importNode($result,true)//将节点复制到其他文档
$items=$docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result)//将复制的节点添加到目标文档
echo$docDest->saveXML();
XPath表达式应该是/items/item[id=1003]
。这是我首先走的路线,它对我很有效。xpath的工作方式完全正确,一切都按其应有的方式进行。巨大的帮助——而且非常容易理解。谢谢
<?php
header('Content-type: application/xml'); //Just to test in the browser directly and have a good format
$docSource = new DOMDocument();
$docSource->loadXML(file_get_contents('source.xml'));
$docDest = new DOMDocument();
$docDest->loadXML(file_get_contents('destination.xml'));
$xpath = new DOMXPath($docSource);
$result = $xpath->query('//item[id=1003]')->item(0); //Get directly the node you want
$result = $docDest->importNode($result, true); //Copy the node to the other document
$items = $docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result); //Add the copied node to the destination document
echo $docDest->saveXML();