Created
November 4, 2012 20:14
-
-
Save apphp-snippets/4013478 to your computer and use it in GitHub Desktop.
This code shows how to parse XML file in easy way using PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* Source: http://www.apphp.com/index.php?snippet=php-parsing-xml-file */ | |
| // this is a sample xml string | |
| $xml_string="<?xml version='1.0'?> | |
| <text> | |
| <article id='Article1'> | |
| <title>Title 1</title> | |
| <content>..text here..</content> | |
| </article> | |
| <article id='Article2'> | |
| <title>Title 2</title> | |
| <content>..text here..</content> | |
| </article> | |
| </text>"; | |
| // load this xml string using simplexml function | |
| $xml = simplexml_load_string($xml_string); | |
| // loop through the each node | |
| foreach($xml->article as $key){ | |
| // attribute are accessted by | |
| echo $key['id'].' '; | |
| // node are accessted by -> operator | |
| echo $key->title.' '; | |
| echo $key->content.'<br />'; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment