我有2个文件1.xml,2.xml两个都有相似的结构,我想有一个.我尝试了很多解决方案,但我只有错误 - 坦率地说,我不知道这些脚本是如何工作的.
1.xml:
<res>
<items total="180">
<item>
<id>1</id>
<title>Title 1</title>
<author>Author 1</author>
</item>
...
</items>
</res>
Run Code Online (Sandbox Code Playgroud)
2.xml:
<res>
<items total="123">
<item>
<id>190</id>
<title>Title 190</title>
<author>Author 190</author>
</item>
...
</items>
</res>
Run Code Online (Sandbox Code Playgroud)
我想创建一个merged.xml具有以下结构的新文件
<res>
<items total="303">
<item>
<id>1</id>
<title>Title 1</title>
<author>Author 1</author>
</item>
... //items from 1.xml
<item>
<id>190</id>
<title>Title 190</title>
<author>Author 190</author>
</item>
... //items from 2.xml
</items>
</res>
Run Code Online (Sandbox Code Playgroud)
我该怎么办?你能解释一下这样做的方法吗?如何使用更多文件?谢谢
编辑
我尝试了什么?
<?php
function mergeXML(&$base, $add)
{
if ( $add->count() != 0 )
$new = $base->addChild($add->getName());
else
$new = $base->addChild($add->getName(), $add);
foreach ($add->attributes() as $a => $b)
{
$new->addAttribute($a, $b);
}
if ( $add->count() != 0 )
{
foreach ($add->children() as $child)
{
mergeXML($new, $child);
}
}
}
$xml = mergeXML(simplexml_load_file('1.xml'), simplexml_load_file('2.xml'));
echo $xml->asXML(merged.xml);
?>
Run Code Online (Sandbox Code Playgroud)
EDIT2
遵循Torious的建议我查看了DOMDocument手册并找到了一个例子:
function joinXML($parent, $child, $tag = null)
{
$DOMChild = new DOMDocument;
$DOMChild->load($child);
$node = $DOMChild->documentElement;
$DOMParent = new DOMDocument;
$DOMParent->formatOutput = true;
$DOMParent->load($parent);
$node = $DOMParent->importNode($node, true);
if ($tag !== null) {
$tag = $DOMParent->getElementsByTagName($tag)->item(0);
$tag->appendChild($node);
} else {
$DOMParent->documentElement->appendChild($node);
}
return $DOMParent->save('merged.xml');
}
joinXML('1.xml', '2.xml')
Run Code Online (Sandbox Code Playgroud)
但它会创建错误的xml文件:
<res>
<items total="180">
<item>
<id>1</id>
<title>Title 1</title>
<author>Author 1</author>
</item>
...
</items>
<res>
<items total="123">
<item>
<id>190</id>
<title>Title 190</title>
<author>Author 190</author>
</item>
...
</items>
</res>
</res>
Run Code Online (Sandbox Code Playgroud)
我无法正确使用此文件.我需要正确的结构,在这里我有一种文件粘贴到另一个文件中.我想"粘贴"只有项目不是所有标签.我应该改变什么?
EDIT3
这是一个答案 - 基于Torious的答案 - 只是根据我的需要调整 - 检查//编辑
$doc1 = new DOMDocument();
$doc1->load('1.xml');
$doc2 = new DOMDocument();
$doc2->load('2.xml');
// get 'res' element of document 1
$res1 = $doc1->getElementsByTagName('items')->item(0); //edited res - items
// iterate over 'item' elements of document 2
$items2 = $doc2->getElementsByTagName('item');
for ($i = 0; $i < $items2->length; $i ++) {
$item2 = $items2->item($i);
// import/copy item from document 2 to document 1
$item1 = $doc1->importNode($item2, true);
// append imported item to document 1 'res' element
$res1->appendChild($item1);
}
$doc1->save('merged.xml'); //edited -added saving into xml file
Run Code Online (Sandbox Code Playgroud)
Tor*_*ous 18
既然你付出了努力,我就编写了一些应该有效的东西.
请注意,这是未经测试的代码,但您明白了.
你可以把它变成漂亮的功能.确保你了解正在发生的事情; 能够使用DOM可能会帮助你在很多未来的场景中.关于DOM标准的一个很酷的事情是,你在许多不同的编程语言/平台上都有相同的操作.
$doc1 = new DOMDocument();
$doc1->load('1.xml');
$doc2 = new DOMDocument();
$doc2->load('2.xml');
// get 'res' element of document 1
$res1 = $doc1->getElementsByTagName('res')->item(0);
// iterate over 'item' elements of document 2
$items2 = $doc2->getElementsByTagName('item');
for ($i = 0; $i < $items2->length; $i ++) {
$item2 = $items2->item($i);
// import/copy item from document 2 to document 1
$item1 = $doc1->importNode($item2, true);
// append imported item to document 1 'res' element
$res1->appendChild($item1);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20268 次 |
| 最近记录: |