簡體   English   中英

將多個XML文件合並為具有不同格式的單個文件

[英]Merging multiple XML files into single file with different format

我有1200多種XML格式,需要將其合並為不同格式的單個XML文件。 各個文件都位於一個目錄中。 我正在使用的服務器具有SimpleXML,並且我嘗試使用一些在網上找到的不同合並示例( http://www.nicolaskuttler.com/post/merging-and-splitting-xml-files-with-simplexml / ,代表一個),但是當我查看“合並的” 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 );
?>

如果這是一項一次性任務,則可以將所有文件連接在一起,然后對連接的文件運行簡單的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文件

不能完全確定錯誤的出處,但是下面的腳本可以幫助您根據規格合並文件:

<?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()一一追加到文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM