doutuo3935 2014-04-10 18:43
浏览 113

使用PHP合并和附加XML文档

I would like to merge two XML documents. They are from two distinct web services. Each XML documents states how many books it holds in the node. I would like the total of (from each XML document) to be added and appended to the resulting merged XML document.

PHP code merging the documents:

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

First XML document:

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

Second XML document:

   <?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条回答 默认 最新

  • donglin4636 2014-04-10 19:06
    关注

    Save the content of your XML documents into variables, e.g. $xmlDocument1, 2, 3...

    Use str_replace (http://www.php.net/manual/de/function.str-replace.php) and delete from the first document.

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

    Then use it for each (use for each!) to remove it from every further document (or just the second one):

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

    $i is the value you set in your for each which shall increase +1 every loop.

    At the end, add :

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

    And last but not least, write it to a new .xml file:

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

    You can also shorten the upper str_replace (in the loop) with an array but I think this is better visualized for you. Give it a try. Good luck!

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符