2011-11-30 24 views
1

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

回答

1

我剛剛這樣做了,我看不清楚究竟是怎麼回事。但我可以提供我爲網站構建的功能,供您查看。
該功能可以100%正常工作,並且和預期的一樣。它創建了一個完美的XML文檔並完美地格式化了它。我希望這可以幫助你找到你的問題。

function create_xml_file() 
{ 
/* create a dom document with encoding utf8 */ 
$domtree = new DOMDocument('1.0', 'utf-8'); 

/* create the root element of the xml tree */ 
/* Data Node */ 
$xmlRoot = $domtree->createElement("data"); 

/* append it to the document created */ 
$xmlRoot = $domtree->appendChild($xmlRoot); 

/* Set our Prices in our <data> <config> node */ 
$config_node = $domtree->createElement("config"); 
$config_node = $xmlRoot->appendChild($config_node); 

// Add - node to config 
$config_node->appendChild($domtree->createElement('config_node', '123456')); 
$config_node->appendChild($domtree->createElement('some_other_data', '123456')); 

/* Create prices Node */ 
$price_node = $domtree->createElement('price'); 
$price_node = $config_node->appendChild($price_node); 

/* Black Price Node */ 
$black_node = $price_node->appendChild($domtree->createElement('black')); 
foreach ($p->List_all() as $item): 
    if ($item['color'] == 'black'): 
     $black_node->appendChild($domtree->createElement($item['type'], $item['price'])); 
    endif; 
endforeach; 

/* Create galleries Node */ 
$galleries_node = $domtree->createElement("galleries"); 
$galleries_node = $xmlRoot->appendChild($galleries_node); 
foreach ($i->List_all() as $image): 
    /* Our Individual Gallery Node */ 
    $gallery_node = $domtree->createElement("gallery"); 
    $gallery_node = $galleries_node->appendChild($gallery_node); 

    $gallery_node->appendChild($domtree->createElement('name', $image['name'])); 
    $gallery_node->appendChild($domtree->createElement('filepath', $image['filepath'])); 
    $gallery_node->appendChild($domtree->createElement('thumb', $image['thumb'])); 
endforeach; 

/* Format it so it is human readable */ 
$domtree->preserveWhiteSpace = false; 
$domtree->formatOutput = true; 

/* get the xml printed */ 
//echo $domtree->saveXML(); 
$file = 'xml/config.xml'; 
$domtree->save($file); 
} 

我希望這可以幫助您找到答案。爲了便於理解,我評論了它。

+0

事情是,我有大量的數據,因此試圖添加節點的bacthes然後b追加到XML到一個文件,而不是將它保存在一個,這將工作?如果是這樣,我該如何整合它?非常感謝 – LeeTee

+0

我試圖追加路線,它可以完成,但我也有某種錯誤(我不記得)。對不起 – Anil

+0

我認爲你需要這個:http://php.net/manual/en/domdocument.importnode.php – Anil

1

我正在使用此命令刪除所有字符串,如<?something?>

  $text = preg_replace('/<\?[^\?>]*\?>/', ' ', $text); 

先擦除它們全部,然後在開始處抹一個。

0

我只是改變

$strxml = $doc->saveXML(); 

fwrite($fh, $strxml); 

$doc->save($file); 

和它完美的作品。