Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP building XML files using DOMDocument in batches
    text
    copied!<p>I have a problem when looping through data and creating XML files using DOMDocument. It all worked fine until I decided to run this script in batches. Now I have multiple <code>'&lt;?xml version="1.0"?&gt;'</code> starting tags in my XML files, looks like one for each batch. There are also more products nodes being generated than there are products. Can anyone help.</p> <pre><code>//get products $productsObj = new Products($db,$shopKeeperID); //find out how many products $countProds = $productsObj-&gt;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-&gt;formatOutput = true; $counter = 1; $r = $doc-&gt;createElement( "products" ); $doc-&gt;appendChild( $r ); for ($i = 0; $i &lt; $countProds; $i += $productBatchLimit) { $limit = $productBatchLimit*$counter; $products = $productsObj-&gt;getShopKeeperProducts($i, $limit); $prod = ''; //loop through each product to create well formed XML foreach( $products as $product ){ $prod = $doc-&gt;createElement( "offer" ); $offerID = $doc-&gt;createElement( "offerID" ); $offerID-&gt;appendChild($doc-&gt;createTextNode( $product['prod_id'] )); $prod-&gt;appendChild( $offerID ); $productName = $doc-&gt;createElement( "name" ); $productName-&gt;appendChild($doc-&gt;createTextNode( $product['productName'] )); $prod-&gt;appendChild( $productName ); $r-&gt;appendChild( $prod ); $strxml = $doc-&gt;saveXML(); } fwrite($fh, $strxml); $counter++; } fclose($fh); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload