我有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