所以我需要使用PHP将3个xml文件合并到一个xml文件中。我对如何做到这一点有点困惑。对此的一些指导将不胜感激。
这些是我需要合并的3个文件:
XML 1
<?xml version="1.0" encoding="UTF-8"?>
<CD_LIST country="USA">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CD_LIST>
XML 2
<?xml version="1.0" encoding="UTF-8"?>
<CD_LIST country="UK UK UK">
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tylor</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
<CD>
<TITLE>Still got the blues</TITLE>
<ARTIST>Gary More</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>10.20</PRICE>
<YEAR>1990</YEAR>
</CD>
<CD>
<TITLE>This is US</TITLE>
<ARTIST>Gary Lee</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>Virgin records</COMPANY>
<PRICE>12.20</PRICE>
<YEAR>1990</YEAR>
</CD>
</CD_LIST>
XML 3
<?xml version="1.0" encoding="UTF-8"?>
<CD_LIST country="AU AU">
<CD>
<TITLE>Whispering Jack</TITLE>
<ARTIST>John Farnham</ARTIST>
<COUNTRY>AU</COUNTRY>
<COMPANY> Farnham's studio </COMPANY>
<PRICE>22.00</PRICE>
<YEAR>1986</YEAR>
</CD>
<CD>
<TITLE>Innocent Eyes</TITLE>
<ARTIST>Delta Goodrem</ARTIST>
<COUNTRY>AU</COUNTRY>
<COMPANY> Delta Goodrem studio </COMPANY>
<PRICE>13.50</PRICE>
<YEAR>2008</YEAR>
</CD>
</CD_LIST>
最终输出必须包含以下行:
<?xml version="1.0" encoding="UTF-8"?>
答案 0 :(得分:1)
因为简单地尝试合并文档存在一个小问题,因为每个文档都有一个具有不同属性的文档根...
<CD_LIST country="AU AU">
<CD_LIST country="UK UK UK">
<CD_LIST country="USA">
所以有两个可能性,一个是创建一个位于其上方的新标签并复制完整内容,或者第二个是仅复制新根元素下的所有CD元素。
这段代码可以在新的根目录下将它们合并在一起......
function mergeFile ( DOMDocument $target, $fileName ) {
$source = new DOMDocument();
$source->load($fileName);
$import = $target->importNode($source->documentElement, true);
$target->documentElement->appendChild($import);
}
$target = new DOMDocument();
$target->loadXML('<?xml version="1.0" encoding="utf-8"?><CD_LIST></CD_LIST>');
mergeFile($target, "NewFile.xml");
mergeFile($target, "NewFile1.xml");
mergeFile($target, "NewFile2.xml");
$target->save("out.xml");
创建一个像......
的文档<?xml version="1.0" encoding="utf-8"?>
<CD_LIST>
<CD_LIST country="USA">
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CD_LIST>
<CD_LIST country="UK UK UK">
<CD>
第二个选项是......
function mergeFile2 ( DOMDocument $target, $fileName ) {
$source = new DOMDocument();
$source->load($fileName);
foreach ( $source->getElementsByTagName("CD") as $cd ) {
$import = $target->importNode($cd, true);
$target->documentElement->appendChild($import);
}
}
$target = new DOMDocument();
$target->loadXML('<?xml version="1.0" encoding="utf-8"?><CD_LIST></CD_LIST>');
mergeFile2($target, "NewFile.xml");
mergeFile2($target, "NewFile1.xml");
mergeFile2($target, "NewFile2.xml");
$target->save("out2.xml");
...创建
<?xml version="1.0" encoding="utf-8"?>
<CD_LIST>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tylor</ARTIST>
<COUNTRY>UK</COUNTRY>