【发布时间】: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 个文件,但我认为我至少应该了解如何在我之前合并两个文件尝试尝试。