使用PHP合并和附加XML文档

时间:2014-04-10 18:43:11

标签: php xml

我想合并两个XML文档。它们来自两个不同的Web服务。每个XML文档都说明它在节点中保存了多少本书。 我希望将(从每个XML文档中)的总数添加并附加到生成的合并XML文档中。

合并文档的PHP代码:

<?php
header('Content-type:  text/xml');

$xmlDom1 = new DOMDocument();
$xmlString = '';
foreach ( file('1.xml') as $node ) {
$xmlString .= trim($node);
}
$xmlDom1->loadXML($xmlString);


$xmlDom2 = new DOMDocument();
$xmlString = '';
foreach ( file('2.xml') as $node ) {
$xmlString .= trim($node);
}
$xmlDom2->loadXML($xmlString);



//merge doms
$xmlRoot = $xmlDom1->documentElement;
foreach ( $xmlDom2->documentElement->childNodes as $node2 ) {
 $node = $xmlDom1->importNode($node2,true);
 $xmlRoot->appendChild($node);

} echo $ xmlDom1-&gt; saveXML();?&gt;

第一个XML文档:

<?xml version="1.0"?>
<catalog>
<NumberOfResult>2</NumberOfResult>
<book>
  <bookid>1</bookid>
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications 
  with XML.</description>
   </book>
  <book>
<bookid>2</bookid>
  <author>Ralls, Kim</author>
  <title>Midnight Rain</title>
  <genre>Fantasy</genre>
  <price>5.95</price>
  <publish_date>2000-12-16</publish_date>
  <description>A former architect battles corporate zombies, 
  an evil sorceress, and her own childhood to become queen 
  of the world.</description>
 </book>
</catalog>

第二个XML文档:

   <?xml version="1.0"?>
<catalog>
<NumberOfResult>1</NumberOfResult>
<book>
<bookid>3</bookid>
  <author>Galos, Mike</author>
  <title>Visual Studio 7: A Comprehensive Guide</title>
  <genre>Computer</genre>
  <price>49.95</price>
  <publish_date>2001-04-16</publish_date>
  <description>Microsoft Visual Studio 7 is explored in depth,
  looking at how Visual Basic, Visual C++, C#, and ASP+ are 
  integrated into a comprehensive development 
  environment.</description>
</book>
</catalog>

2 个答案:

答案 0 :(得分:1)

你有两个任务。将书籍节点从多个XML文档导入到一个文档中,并将计数作为元素添加到它们之前。

首先创建目标文档并添加目录节点:

$dom = new DOMDocument();
$catalog = $dom->appendChild($dom->createElement('catalog'));

接下来加载每个源文档,获取图书节点并将其导入目标文档。

foreach ($xmls as $xml) {
  $srcDom = new DOMDocument();
  $srcDom->loadXml($xml);
  $xpath = new DOMXpath($srcDom);
  foreach ($xpath->evaluate('/catalog/book') as $book) {
    $catalog->appendChild($dom->importNode($book, TRUE));
  }
}

现在所有书籍节点都在目标文档中,您可以使用Xpath来计算它们。

$xpath = new DOMXpath($dom);
$count = $dom->createElement('NumberOfResult');
$count->appendChild(
  $dom->createTextNode(
    $xpath->evaluate('count(/catalog/books)')
  )
);
$catalog->insertBefore($count, $catalog->firstChild);

现场演示:https://eval.in/135143

答案 1 :(得分:-1)

将XML文档的内容保存到变量中,例如$ xmlDocument1,2,3 ......

使用str_replace(http://www.php.net/manual/de/function.str-replace.php)并从第一个文档中删除。

$output_new = str_replace("</catalog>", "", $xmlDocument1);

然后将它用于每个(用于每个!)将其从每个进一步的文档(或仅仅是第二个)中删除:

$output_new = $output_new . str_replace("<catalog>", "", $xmlDocument[$i]);
$output_new = $output_new . str_replace("</catalog>", "", $xmlDocument[$i]);

$ i是您在每个循环中设置的值,每个循环都会增加+1。

最后,添加:

$output_new = $output_new . '</catalog>';

最后但并非最不重要的是,将其写入新的.xml文件:

$xmlDocument_new = fopen("merged.xml","w");
fwrite($xmlDocument_new, $output_new);

您还可以使用数组缩短上面的str_replace(在循环中),但我认为这样可以更好地为您显示。试试看。祝你好运!