我在使用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