Posted By

paulgrenwood on 09/23/09


Tagged

php parse xml


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

m0d


Parse XML Data


 / Published in: PHP
 

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

Report this snippet  

You need to login to post a comment.