Created
February 20, 2011 22:54
-
-
Save joshhartman/836401 to your computer and use it in GitHub Desktop.
Parse XML with SimpleXML
This file contains 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 | |
//this is a sample xml string | |
$xml_string="<?xml version='1.0'?> | |
<moleculedb> | |
<molecule name='Benzine'> | |
<symbol>ben</symbol> | |
<code>A</code> | |
</molecule> | |
<molecule name='Water'> | |
<symbol>h2o</symbol> | |
<code>K</code> | |
</molecule> | |
</moleculedb>"; | |
//load the xml string using simplexml function | |
$xml = simplexml_load_string($xml_string); | |
//loop through the each node of molecule | |
foreach ($xml->molecule as $record) | |
{ | |
//attribute are accessted by | |
echo $record['name'], ' - '; | |
//node are accessted by -> operator | |
echo $record->symbol, ' - '; | |
echo $record->code, '<br />'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment