在PHP中合并XML文件

我有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>

2.xml

<res>

<items total="123">

<item>

<id>190</id>

<title>Title 190</title>

<author>Author 190</author>

</item>

...

</items>

</res>

我想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>

我应该怎么做?你能解释一下方法吗?如何处理更多文件?谢谢

我尝试了什么?

<?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);

?>

遵循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')

但是它创建了错误的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>

而且我无法正确使用此文件。我需要正确的结构,在这里我可以将一个文件粘贴到另一个文件中。我只想“粘贴”项目的所有标签。我应该改变什么?

编辑3

这是一个答案-基于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

回答:

既然您付出了很大的努力,我已经编写了一些应该起作用的代码。

请注意,这是未经测试的代码,但是您可以理解。

使它具有漂亮的功能取决于您。确保您了解发生了什么事;能够使用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);

}

以上是 在PHP中合并XML文件 的全部内容, 来源链接: utcz.com/qa/416838.html

回到顶部