i'm parsing a xml file. When i output one attribute for instance, i always get the result 2 times.
here is some simplified code of what i do:
$xml = new XMLReader();
$xml->open($file);
while ($xml->read()) {
if ($xml->name == 'file')
echo $xml->getAttribute ('Product_ID') . '<br />';
}
// close stream
$xml->close();
this is what i get:
1980 1980 37444 37444 45287 45287 65438 65438 76916 76916 101158 101158 271287 271287
XML Structure is the following:
<file path="export/freexml.int/DE/15986140.xml" Product_ID="15986140" Updated="20121114141132" Quality="ICECAT" Supplier_id="728" Prod_ID="RBBD2MZ" Catid="2282" On_Market="0" Model_Name="ThinkCentre Edge 92z" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15916192-2729.jpg" HighPicSize="12635" HighPicWidth="337" HighPicHeight="294" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986142.xml" Product_ID="15986142" Updated="20121114143018" Quality="ICECAT" Supplier_id="24" Prod_ID="NX.C0ZEB.002" Catid="151" On_Market="0" Model_Name="TE11HC-32376G50Mnks" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986142-574.jpg" HighPicSize="179174" HighPicWidth="786" HighPicHeight="621" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986149.xml" Product_ID="15986149" Updated="20121114144736" Quality="ICECAT" Supplier_id="24" Prod_ID="NX.C1UEB.001" Catid="151" On_Market="0" Model_Name="LE11-BZ-E1124G50Mn" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986149-2702.jpg" HighPicSize="205805" HighPicWidth="786" HighPicHeight="621" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986153.xml" Product_ID="15986153" Updated="20121114200420" Quality="ICECAT" Supplier_id="1935" Prod_ID="50203" Catid="194" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986153-3865.jpg" HighPicSize="1928713" HighPicWidth="2751" HighPicHeight="1897" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986154.xml" Product_ID="15986154" Updated="20121114200048" Quality="ICECAT" Supplier_id="1935" Prod_ID="ARMAKB" Catid="194" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986154-7619.jpg" HighPicSize="1928713" HighPicWidth="2751" HighPicHeight="1897" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986155.xml" Product_ID="15986155" Updated="20121114194744" Quality="ICECAT" Supplier_id="1935" Prod_ID="ARMAM" Catid="195" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986155-4238.jpg" HighPicSize="639005" HighPicWidth="2201" HighPicHeight="3265" Date_Added="20121114000000"></file>
<file path="export/freexml.int/DE/15986156.xml" Product_ID="15986156" Updated="20121114194735" Quality="ICECAT" Supplier_id="1935" Prod_ID="54577" Catid="195" On_Market="0" Model_Name="Arma" Product_View="0" HighPic="http://images.icecat.biz/img/norm/high/15986156-7292.jpg" HighPicSize="639005" HighPicWidth="2201" HighPicHeight="3265" Date_Added="20121114000000"></file>
as you can see every number is displayed twice. i don't get the problem -.-. what am i doing wrong?
thank you very much for your help guys!
// EDIT
ok, i fixed it this way:
if ($xml->name == 'file' && $xml->nodeType == XMLReader::ELEMENT)
Thanks for the help !
Found this in the comments in the docs:
might be obvious, but not to everyone ;-) ... when reading attributes from a node that has sub-nodes (and creating an output from this node), the output will be issued twice, once on the tag and once on the end tag . To avoid this, you can test on which part of the node you are using the property nodeType. It'll be 1 for the element, 15 for the end element.
http://www.php.net/manual/en/xmlreader.getattribute.php
You can either apply the above proposed solution or go with another algorithm for traversing the nodes like shown here: http://www.w3schools.com/php/php_xml_simplexml.asp
<?php
$xml = simplexml_load_file("test.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
{
echo $child->getName() . ": " . $child . "<br />";
}
?>
UPDATE Amended your code with the proposed solution in the comments.
$xml = new XMLReader();
$xml->open($file);
while ($xml->read()) {
if ($xml->name == 'file' && $xml->nodeType==XMLReader::ELEMENT)
echo $xml->getAttribute ('Product_ID') . '<br />';
}
// close stream
$xml->close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With