PHP使用DOMDocument批量构建XML文件


PHP building XML files using DOMDocument in batches

我在使用DOMDocument遍历数据和创建XML文件时遇到了一个问题。这一切都工作得很好,直到我决定批量运行这个脚本。现在我的XML文件中有多个'<?xml version="1.0"?>'起始标记,看起来每个批都有一个。生成的产品节点也比产品多。有人能帮忙吗?

//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);

我使用这个命令来擦除所有字符串,如<?something?>

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

先把它们全部擦掉,然后在开头放一个

我刚才就这样做了,我看不出到底出了什么问题。但我可以提供我为网站构建的功能供您查看。
这个函数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);
}

我希望这有助于你找到你的答案。

我只是改变了

$strxml = $doc->saveXML(); 

fwrite($fh, $strxml);  

$doc->save($file);

效果很好