Posted By

hasantayyar on 08/18/09


Tagged

php parse xml


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

umang_nine
leecsargent
BrianCoyDesign
tlongren
mloberg


PHP parse XML


 / Published in: PHP
 

  1. <?php
  2. //xml string
  3. $xml_string="<?xml version='1.0'?>
  4. <users>
  5. <user id='398'>
  6. <name>Foo</name>
  7. <email>[email protected]</name>
  8. </user>
  9. <user id='867'>
  10. <name>Foobar</name>
  11. <email>[email protected]</name>
  12. </user>
  13. </users>";
  14.  
  15. //load the xml string using simplexml
  16. $xml = simplexml_load_string($xml_string);
  17.  
  18. //loop through the each node of user
  19. foreach ($xml->user as $user)
  20. {
  21. //access attribute
  22. echo $user['id'], ' ';
  23. //subnodes are accessed by -> operator
  24. echo $user->name, ' ';
  25. echo $user->email, '<br />';
  26. }
  27.  
  28.  
  29. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: shaamil on January 4, 2010

very helpful code snippet. however I need help on the following.

Sample xml

20100103 22:44

PG 
Bangkok Airways 
PG 711 

How do I get the value for 'Updatetime' ?

Posted By: shaamil on January 4, 2010

Oh no! I didn't know that my mark up will be trimmed down.. Please ignore the above comment..

You need to login to post a comment.