【问题标题】:Merging multiple XML files into single file with different format将多个 XML 文件合并为具有不同格式的单个文件
【发布时间】:2011-04-20 19:19:48
【问题描述】:

我有 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 );
?>

【问题讨论】:

  • 你能给你的 xml 合并代码吗?
  • 我刚刚添加了用于合并两个文件的代码 - 我还没有确定如何合并所有 1200 个文件,但我认为我至少应该了解如何在我之前合并两个文件尝试尝试。

标签: php xml simplexml


【解决方案1】:

如果这是一项一次性任务,那么您可以将所有文件连接在一起,然后对连接的文件运行一个简单的 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 文件

【讨论】:

    【解决方案2】:

    不确定你在哪里犯了错误,但下面是可以帮助你根据规范合并文件的脚本:

    <?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-&gt;asXML() 一个一个地附加到文件中。

    【讨论】:

    • 我需要将数据从一个 CMS 获取到另一个 CMS,而旧的 CMS 将每个内容都做成了自己的文件。新的需要一个用于导入的文件。
    • 输出将是一个文件,因为它是附加的。我只是建议不要在内存中构造$xml
    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-28
    • 2015-04-12
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多