【发布时间】:2018-07-06 19:15:54
【问题描述】:
自从一个星期以来,我一直在为这个问题而烦恼:我正在尝试使用 API 下载和动态合并多个 xml 文件。我可以下载所有文件,但如果没有多个根元素,我就无法合并它们……这令人沮丧,我找不到任何建议。这是我的代码:
<?php
$fileout = 'file.xml';
unlink($fileout);
$baseurl="https://websitewithapi.com/";
$topcategories=array("COOL","DRIVE","FUN");
foreach ($topcategories as $topcategory) {
$url_cata_test="https://websitewithapi.com/&filters=topcategory:$topcategory&limit=1";
$jsontest = file_get_contents($url_cata_test);
$arrtest=json_decode($jsontest);
$items=$arrtest->pagination->count;
$pagemax=ceil($items/250);
$pagetest= range(0,$pagemax);
foreach ($pagetest as $page) {
$url_cata="$baseurl&filters=topcategory:$topcategory&offset=$page&limit=250";
echo "Cat en cours d import: ".$topcategory."\n";
echo "Page en cours d import: ".$page."\n";
echo "URL Cata: $url_cata \n";
};
$dom = new DOMDocument();
$dom->appendChild($dom->createElement('superdeals'));
$files= array($url_cata);
foreach ($files as $filename) {
$addDom = new DOMDocument();
$addDom->load($filename);
if ($addDom->documentElement->getElementsByTagName('products')) {
foreach ($addDom->documentElement->getElementsByTagName('product') as $node) {
$dom->documentElement->appendChild(
$dom->importNode($node, TRUE)
);
}
}
$dom->formatOutput = true;
file_put_contents($fileout, $dom->saveXML(), FILE_APPEND);
}
};
?>
我总是在同一个文件中“关联”文件但有多个根时遇到同样的问题!有什么我想念的吗?
谢谢。
【问题讨论】:
标签: php xml merge domdocument