Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8853199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:35:35+00:00 2026-06-14T13:35:35+00:00

i’m parsing a xml file. When i output one attribute for instance, i always

  • 0

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:35:36+00:00Added an answer on June 14, 2026 at 1:35 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an XML file, the creators of it stuck in a bunch social
I have just tried to save a simple *.rtf file with some websites and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.