Parsing XML using PHP

Submitted by:Jhon Brain

Date added:04 October, 2012


Code example for parsing XML in easy way using PHP

Tags: xml parsing

Code Snippet:

//this is a sample xml string
$xml_string="<?xml version='1.0'?>
<molecule name='Benzine'>
<molecule name='Water'>

//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 />';