我在使用DOMDocument循环访问数据和创建XML文件时遇到了问题。它一切正常,直到我决定批量运行这个脚本。现在我在我的XML文件中有多个'<?xml version="1.0"?>'
开始标记,看起来像每个批处理一个。产品节点也比产品多。任何人都可以帮忙PHP使用DOMDocument批量构建XML文件
//get products
$productsObj = new Products($db,$shopKeeperID);
//find out how many products
$countProds = $productsObj->countProducts();
$productBatchLimit = 3; //keep low for testing
//create new file
$file = 'products/'. $products . '.xml';
$fh = fopen($file, 'a');
//create XML document object model (DOM)
$doc = new DOMDocument();
$doc->formatOutput = true;
$counter = 1;
$r = $doc->createElement("products");
$doc->appendChild($r);
for ($i = 0; $i < $countProds; $i += $productBatchLimit) {
$limit = $productBatchLimit*$counter;
$products = $productsObj->getShopKeeperProducts($i, $limit);
$prod = '';
//loop through each product to create well formed XML
foreach($products as $product){
$prod = $doc->createElement("offer");
$offerID = $doc->createElement("offerID");
$offerID->appendChild($doc->createTextNode($product['prod_id']));
$prod->appendChild($offerID);
$productName = $doc->createElement("name");
$productName->appendChild($doc->createTextNode($product['productName']));
$prod->appendChild($productName);
$r->appendChild($prod);
$strxml = $doc->saveXML();
}
fwrite($fh, $strxml);
$counter++;
}
fclose($fh);
事情是,我有大量的数据,因此试图添加节点的bacthes然后b追加到XML到一个文件,而不是将它保存在一个,这将工作?如果是这样,我该如何整合它?非常感谢 – LeeTee
我试图追加路线,它可以完成,但我也有某种错误(我不记得)。对不起 – Anil
我认为你需要这个:http://php.net/manual/en/domdocument.importnode.php – Anil