2011-04-20 154 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

相關問題