Parsing XML File with PHP


/ Published in: PHP
Save to your folder(s)

This code shows how to parse XML file in easy way using PHP.


Copy this code and paste it in your HTML
  1. <?php
  2. // this is a sample xml string
  3. $xml_string="<?xml version='1.0'?>
  4. <text>
  5. <article id='Article1'>
  6. <title>Title 1</title>
  7. <content>..text here..</content>
  8. </article>
  9. <article id='Article2'>
  10. <title>Title 2</title>
  11. <content>..text here..</content>
  12. </article>
  13. </text>";
  14.  
  15. // load this xml string using simplexml function
  16. $xml = simplexml_load_string($xml_string);
  17.  
  18. // loop through the each node
  19. foreach($xml->article as $key){
  20. // attribute are accessted by
  21. echo $key['id'].' ';
  22. // node are accessted by -> operator
  23. echo $key->title.' ';
  24. echo $key->content.'<br />';
  25. }
  26. ?>

URL: http://www.apphp.com/index.php?snippet=php-parsing-xml-file

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.