2011-04-20 65 views
1

我有1200多个XML格式,我需要合并到一个不同格式的单个XML文件中。单个文件都位于一个目录中。我正在使用的服务器有SimpleXML,我尝试过使用我在网上找到的一些不同的合并示例(例如http://www.nicolaskuttler.com/post/merging-and-splitting-xml-files-with-simplexml/),但是当我查看“合并的”XML文件时,只有第一个XML文件被添加到它。我没有能够获得多个文件中的任何一个与我的几次尝试“合并”。将多个XML文件合并为具有不同格式的单个文件

格式的单个文件:

<?xml version="1.0" encoding="UTF-8"?> 
<pr:press_release xmlns:alf="http://www.alfresco.org" xmlns:chiba="http://chiba.sourceforge.net/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:pr="http://www.bowl.com/pr" xmlns:xf="http://www.w3.org/2002/xforms" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <pr:headline>TITLE</pr:headline> 
     <pr:title>TITLE</pr:title> 
     <pr:contact_info xsi:nil="true"/> 
     <pr:department>DEPT</pr:department> 
     <pr:body>BODY</pr:body> 
     <pr:launch_date>YYYY-MM-DD</pr:launch_date> 
     <pr:expiration_date>YYYY-MM-DD</pr:expiration_date> 
     <pr:category>CATEGORY</pr:category> 
     <pr:tags>KEYWORDS</pr:tags> 
</pr:press_release> 

格式需要新的文件:

<?xml version="1.0" encoding="utf-8"?> 
<contents> 
    <content> 
    <title>TITLE</title> 
    <summary></summary> 
    <body> 
     <root> 
     <date></date> 
     <author></author> 
     <department></department> 
     <location></location> 
     <story>BODY</story> 
     </root> 
    </body> 
    </content> 
</contents> 

代码用于合并两个文件:

<?php 
     $file1 = '1027coachintermediate.xml'; 
     $file2 = '1027coachelite.xml'; 
     $fileout = 'fileout.xml';  $xml1 = simplexml_load_file($file1); 
     $xml2 = simplexml_load_file($file2); // loop through the FOO and add them and their attributes to xml1 
     foreach($xml2->FOO as $foo) { 
       $new = $xml1->addChild('FOO' , $foo); 
       foreach($foo->attributes() as $key => $value) { 
         $new->addAttribute($key, $value); 
       } 
     }  $fh = fopen($fileout, 'w') or die ("can't open file $fileout"); 
     fwrite($fh, $xml1->asXML()); 
     fclose($fh); 
?> 
+0

你可以给你的XML合并代码? – 2011-04-20 21:35:06

+0

我刚刚在我用来合并两个文件的代码中添加 - 我还没有确定如何将所有1200合并,但我想我应该至少了解如何在尝试尝试之前合并两个文件。 – chemqueen 2011-04-21 14:37:25

回答

0

如果这是一个单然后您可以将所有文件连接在一起,然后在连接文件上运行简单的XSLT流程。

1)shell脚本连接文件

for file in `ls $XMLDIR` 
    do 
     cat $file | grep -v "xml version" >> big_concat_file.xml 
    done 

2)手动编辑CONCAT文件把根包装标签。

<document> 
    <pr:press-release> 
     .... 
    </pr:press-release> 
    <pr:press-release> 
     ... 
    </pr:press-release> 
</document> 

3)连结文件运行XSLT文件

0

没有真正知道你在哪里做的错误,但下面是脚本,应该可以帮助您按照规格合并文件:

<?php 
$files = array('in1.xml', 'in2.xml'); 

$xml = new SimpleXMLElement(<<<XML 
<?xml version="1.0" encoding="utf-8"?> 
<contents> 
</contents> 
XML 
); 

foreach($files as $filename) { 
    $xml_int = simplexml_load_file($filename); 
    $conts = $xml_int->children('pr',true); 
    $content = $xml->addChild('content'); // add content 
    $content->addChild('title',$conts->title); // add first title 
    // add the rest of the content insides 
    // ... 
} 
var_export($xml->asXML()); 
?> 

输出

<?xml version="1.0" encoding="utf-8"?>    
<contents><content><title>TITLE1</title></content><content><title>TITLE2</title></content></contents> 

看到:http://pl.php.net/manual/en/simplexml.examples-basic.php更多信息

另一个问题是,如果你真的想保留整个xml在内存中。您可以将$content->asXML()逐个追加到文件中。

+0

我需要从一个CMS获取数据到另一个,而旧数据库将每个内容作为自己的文件。新的需要一个文件进行导入。 – chemqueen 2011-04-22 13:21:42

+0

输出将是一个文件,并附上它。我只是建议不要在内存中构建'$ xml'。 – 2011-04-22 17:05:04

相关问题