尝试了很多不同的脚本并最终得到了一个我认为必须接近解决方案的脚本,但不知道如何定义孩子。试过“@name”和“名字”。 我尝试过的所有脚本都是从这里和那里复制过来的,并经过修改后供我使用。
XML有1个节点,每个节点有1个子节点。这不应该比这更容易,但没有运气。 可以提一下,我也希望合并xsl样式表引用,但我想我只是为此解决了一个解决方案,但忘了给页面添加书签。
xml 1:
<?xml version="1.0" encoding="utf-8"?>
<companyroutes>
<route name="BBUPLOAD">ENGM OKSAT L996 SVD EKCH</route>
</companyroutes>
xml 2:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xml" href="merge2.xsl"?>
<companyroutes>
<route name="XXXXCORE">RUTA TULLA L666 BARE LITT MNOP</route>
</companyroutes>
PHP代码(此文件与输出文件位于同一目录中:
<?php
$doc1 = new DOMDocument();
$doc1->load('/uploads/companyroutes.xml');
$doc2 = new DOMDocument();
$doc2->load('/uploads/companyroutes_core.xml');
// get 'res' element of document 1
$res1 = $doc1->getElementsByTagName('res')->name(0);
// iterate over '@name' elements of document 2
$companyroutes2 = $doc2->getElementsByTagName('name');
for ($i = 0; $i < $companyroutes2->length; $i ++) {
$name2 = $companyroutes2->name($i);
// import/copy @name from document 2 to document 1
$name1 = $doc1->importNode($name2, true);
// append imported @name to document 1 'res' element
$res1->appendChild($name1);
}
$doc1->save('companyroutes.xml');
?>
任何指示赞赏。
答案 0 :(得分:1)
以下是将文件与simplexml
合并的方法:
$xml = simplexml_load_string($x1); // assume XML in $x1 and $x2
$xml2 = simplexml_load_string($x2);
foreach ($xml2->route as $route) { // merge $xml2 into $xml
$newroute = $xml->addChild("route", $route);
$newroute->addAttribute("name", $route['name']);
}
unset($xml2); // dispose $xml2
echo $xml->asXML();
看到它有效:https://eval.in/97372
答案 1 :(得分:1)
两个步骤,准备目标DOM(添加文档元素)。加载每个源XML,遍历文档元素中的子节点并将它们导入到目标文档中:
$xmlFiles = [
'/uploads/companyroutes.xml',
'/uploads/companyroutes_core.xml'
];
$targetDom = new DOMDocument();
$targetDom->appendChild(
$targetDom->createProcessingInstruction(
'xml-stylesheet', 'type="text/xml" href="merge2.xsl"'
)
);
$rootNode = $targetDom->appendChild(
$targetDom->createElement('companyroutes')
);
foreach ($xmlFiles as $xmlFile) {
$importDom = new DOMDocument();
$importDom->load($xmlFile);
foreach ($importDom->documentElement->childNodes as $node) {
$rootNode->appendChild($targetDom->importNode($node, TRUE));
}
}
echo $targetDom->saveXml();
使用XML字符串作为来源的示例:https://eval.in/97405
当然,您可以为目标DOM创建模板XML并加载它:
$template = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xml" href="merge2.xsl"?>
<companyroutes/>
XML;
$targetDom = new DOMDocument();
$targetDom->loadXml($template);
$rootNode = $targetDom->documentElement;
...
我不建议更改文档。如果您构建新文档,则可以重复该操作。
答案 2 :(得分:0)
感谢。我做了一些编辑并让它运行。
$xml = simplexml_load_file('./uploads/companyroutes_core.xml'); // xml files URL insted of $x
$xml2 = simplexml_load_file('./uploads/companyroutes.xml');
foreach ($xml2->route as $route) { // merge $xml2 into $xml
$newroute = $xml->addChild("route", $route);
$newroute->addAttribute("name", $route['name']);
}
unset($xml2); // dispose $xml2
$xml->asXML('merged.xml'); //this line saves to directory and name of choice
我现在唯一的问题是所有Childs都在一行输出。 4Mb长线。需要它更整洁(是的,我读过关于整洁的命令,但如何在这里使用它??idunno)并为每个孩子打破它。感谢您的耐心到目前为止。如果你看到我提出更多基本问题,请不要吝啬,但好处是我每天都在学习更多的东西。