PHP批量使用DOMDocument构建XML文件

PHP批量使用DOMDocument构建XML文件,php,domdocument,Php,Domdocument,我在使用DOMDocument循环数据和创建XML文件时遇到问题。在我决定分批运行这个脚本之前,一切都很顺利。现在我的XML文件中有多个'起始标记,看起来每个批都有一个。生成的产品节点也比生成的产品多。有人能帮忙吗 //get products $productsObj = new Products($db,$shopKeeperID); //find out how many products $countProds = $p

我在使用DOMDocument循环数据和创建XML文件时遇到问题。在我决定分批运行这个脚本之前,一切都很顺利。现在我的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);
//获取产品
$productsObj=新产品($db,$shopKeeperID);
//找出有多少种产品
$countProds=$productsObj->countProducts();
$productBatchLimit=3//保持低位以备测试
//创建新文件
$file='products/'$产品。xml';
$fh=fopen($file,'a');
//创建XML文档对象模型(DOM)
$doc=新的DOMDocument();
$doc->formatOutput=true;
$counter=1;
$r=$doc->createElement(“产品”);
$doc->appendChild($r);
对于($i=0;$i<$countProds;$i+=$productBatchLimit){
$limit=$productBatchLimit*$counter;
$products=$productsObj->getShopKeeperProducts($i,$limit);
$prod='';
//循环遍历每个产品以创建格式良好的XML
foreach($products作为$product){
$prod=$doc->createElement(“报价”);
$offerID=$doc->createElement(“offerID”);
$offerID->appendChild($doc->createTextNode($product['prod_id']);
$prod->appendChild($offerID);
$productName=$doc->createElement(“名称”);
$productName->appendChild($doc->createTextNode($product['productName']);
$prod->appendChild($productName);
$r->appendChild($prod);
$strxml=$doc->saveXML();
}           
fwrite($fh,$strxml);
$counter++;
}       
fclose($fh);

我刚才做了这个,我看不出到底出了什么问题。但我可以提供我为网站创建的功能供您查看。
该功能工作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);
}
函数创建xml文件()
{
/*创建编码为utf8的dom文档*/
$domtree=新的DOMDocument('1.0','utf-8');
/*创建xml树的根元素*/
/*数据节点*/
$xmlRoot=$domtree->createElement(“数据”);
/*将其附加到创建的文档中*/
$xmlRoot=$domtree->appendChild($xmlRoot);
/*在我们的节点中设置价格*/
$config_node=$domtree->createElement(“配置”);
$config\u node=$xmlRoot->appendChild($config\u node);
//将-node添加到配置中
$config_node->appendChild($domtree->createElement('config_node','123456');
$config_node->appendChild($domtree->createElement('some_other_data','123456');
/*创建价格节点*/
$price_node=$domtree->createElement('price');
$price\u node=$config\u node->appendChild($price\u node);
/*黑价格节点*/
$black_node=$price_node->appendChild($domtree->createElement('black');
foreach($p->List_all()作为$item):
如果($item['color']=='black'):
$black_node->appendChild($domtree->createElement($item['type'],$item['price']);
endif;
endforeach;
/*创建库节点*/
$galleries_node=$domtree->createElement(“库”);
$galleries\u node=$xmlRoot->appendChild($galleries\u node);
foreach($i->List_all()为$image):
/*我们的个人画廊节点*/
$gallery_node=$domtree->createElement(“gallery”);
$gallery\u node=$gallery\u node->appendChild($gallery\u 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;
/*将其格式化,使其可读*/
$domtree->preserveWhiteSpace=false;
$domtree->formatOutput=true;
/*打印xml*/
//echo$domtree->saveXML();
$file='xml/config.xml';
$domtree->save($file);
}

我希望这能帮助你找到答案。为了便于理解,我对它进行了很好的注释。

我正在使用这个命令来删除所有字符串,如

$text=preg\u replace('/]*\?>/',''.$text);
首先把它们全部擦掉,然后在开头放一个。

我只是简单地改变了

$strxml = $doc->saveXML(); 


它工作得很好。

问题是,我有大量的数据,因此试图添加大量的节点,然后将XML附加到一个文件中,而不是将其保存在一个文件中,这样做行吗?如果是,我如何合并它?很多感谢我尝试了附加路径,它可以完成,但我也有一些错误(我不记得了)。Sorry导入节点,然后使用上面的方法添加到其中。感谢您的帮助,我使用了saveXML()的save()函数inseatd,这似乎很有效。谢谢你的帮助。
$strxml = $doc->saveXML(); 
fwrite($fh, $strxml);  
$doc->save($file);