我有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);
?>
你可以给你的XML合并代码? – 2011-04-20 21:35:06
我刚刚在我用来合并两个文件的代码中添加 - 我还没有确定如何将所有1200合并,但我想我应该至少了解如何在尝试尝试之前合并两个文件。 – chemqueen 2011-04-21 14:37:25