2011-11-30 30 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); 

和它完美的作品。