Reputation: 163
This code is producing inefficent results when I use it to parse a large XML file.
The XML that is parsed looks like this:
<product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>
<categoryPath><category><name>Buy</name></category>
<category><name>Car, Marine & GPS</name></category>
<category><name>Car Audio</name></category>
<category><name>Car Stereos</name></category>
<category><name>CD Decks</name></category></categoryPath>
</product>
There is about 100 sets of product (so basically the above xml times 100)
This code works when there is only about 3-5 sets product, but not when it increases in size. Why doesn't it work for bigger files?
<?php
set_time_limit(0);
// load up your XML
$xml = new DOMDocument;
$xml->load('file.xml');
// Array to store them
$append = array();
foreach ($xml->getElementsByTagName('product') as $product )
{
foreach($product->getElementsByTagName('name') as $name ) {
// Stick $name onto the array
$append[] = $name;
}
// Now append all of them to product
foreach ($append as $a)
{
$product->appendChild($a);
}
$product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}
// final result:
$result = $xml->saveXML();
echo $result;
$file = "new_file.xml";
file_put_contents($file,$result);
?>
After this code is executed, the XML file is supposed to look like this for each product set
<?xml version='1.0'?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name></category>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
</product>
</products>
However, when I use this PHP code to parse a rather large xml file (one with 100 sets of product), it takes the categoryPath node and its children and appends them to the bottom of the file disregarding the node it is supposed to be in (product) If I only parse a small XML file (one that has 3 sets of product), then I will get the desired result (the above XML code is was this PHP code is supposed to do, but it doesnt work when there is a large file).
When I try to parse an XML file that has 100 product sets the result looks like this:
<?xml version='1.0'?>
<products>
<product>
<ItemId>531670</ItemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
</product>
</products>
<name>Buy</name></category>
<name>Car, Marine & GPS</name>
<name>Car Installation Parts</name>
<name>Deck Installation Parts</name>
<name>Antennas & Adapters</name>
Each of the name nodes are not appended inside the product node.
Upvotes: 2
Views: 406
Reputation: 2298
Does this work?
// load up your XML
$xml = new DOMDocument;
$xml->loadXml('
<products>
<product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>
<categoryPath><category><name>Buy</name></category>
<category><name>Car, Marine & GPS</name></category>
<category><name>Car Audio</name></category>
<category><name>Car Stereos</name></category>
<category><name>CD Decks</name></category></categoryPath>
</product>
<product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>
<categoryPath><category><name>Buy</name></category>
<category><name>Car, Marine & GPS</name></category>
<category><name>Car Audio</name></category>
<category><name>Car Stereos</name></category>
<category><name>CD Decks</name></category></categoryPath>
</product>
</products>
');
// Array to store them
foreach ($xml->getElementsByTagName('product') as $product )
{
$append = array();
foreach($product->getElementsByTagName('name') as $name ) {
// Stick $name onto the array
$append[] = $name;
}
foreach ($append as $a) {
// Now append all of them to product
$product->appendChild($a);
}
$product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}
// final result:
$result = $xml->saveXML();
echo '<pre>'.print_r(htmlspecialchars($result),1).'</pre>';
Tested on c. 100 <product>
tags with the following result:
<?xml version="1.0"?>
<products>
<product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Audio</name>
<name>Car Stereos</name>
<name>CD Decks</name>
</product>
<product>
<ItemId>1576829</ItemId>
<modelNumber>CX501</modelNumber>
<name>Buy</name>
<name>Car, Marine & GPS</name>
<name>Car Audio</name>
<name>Car Stereos</name>
<name>CD Decks</name>
</product>
// etc
</products>
Upvotes: 1