PHP parse XML nodes with same name

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I get an xml response from an api I'm using.
Code: [ Select ]
<qdbapi>
<table>
<records>
    <record>
     <f id="3">31129</f>
     <f id="8">Ronald</f>
     <f id="9">Crocker</f>
     <update_id>1291750076076</update_id>
    </record>
   </records>
  </table>
</qdbapi>
  1. <qdbapi>
  2. <table>
  3. <records>
  4.     <record>
  5.      <f id="3">31129</f>
  6.      <f id="8">Ronald</f>
  7.      <f id="9">Crocker</f>
  8.      <update_id>1291750076076</update_id>
  9.     </record>
  10.    </records>
  11.   </table>
  12. </qdbapi>


The problem is once I get to the <record> node I'm not sure how to identify each <f> node. I don't really need the attribute, but I could use it.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

Using simplexml_load_string will give you an object, you can then look at how the object is formatted to get an understanding of how to move through it.

Code: [ Select ]
$xml = simplexml_load_string($string);
print_r($xml);
  1. $xml = simplexml_load_string($string);
  2. print_r($xml);
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

I got it going. Thanks SpooF. The xml was contained in a file and I was using simplexml_load_file() but it was not working. So, my parsing was correct but my data wasn't actually there. I had another script I was pulling the code from and I didn't have any problems with that script. So, I fopened the xml file and fread the data and then used _load_string(). Not sure why _load_file didn't work. I thought permissions but fopen opened the file. Anyway, once I was able to get the structure I was able to move through it.

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 157 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.