Parsing the XML in easy way using PHP

Parsing the XML has been a tough task among the programmer of PHP. I frequently get questions from my friend and via email “How can we parse XML in an easy way? It seems to be a tough task”. If SimpleXML extension is loaded in PHP then it’s not a tough task. But, keep in mind that SimpleXML extension of PHP is only available from the PHP 5.0.

How to check weather simpleXML extension is loaded or not?

You can use the “phpinfo()” function available in PHP to know weather “SimpleXML” extension is loaded or not in PHP. You can see a separate section “SimpleXML” with detail information about this on “phpinfo()”.

Example of parsing xml in easy way using SimpleXML functions

First of all let , look at the simple xml format of the molecule database which I’m going to parse in our example,

<?xml version='1.0'?>
<moleculedb>
    <molecule name='Alanine'>
        <symbol>ala</symbol>
        <code>A</code>
    </molecule>
    <molecule name='Lysine'>
        <symbol>lys</symbol>
        <code>K</code>
    </molecule>
</moleculedb>

As you can see, I’ve included both attribute and value of the element in above XML structure. Now, I’ll show you How can we parse the above XML data in an easy way using PHP

PHP code to parse XML using SimpleXML

//this is a sample xml string
$xml_string="<?xml version='1.0'?>
<moleculedb>
    <molecule name='Alanine'>
        <symbol>ala</symbol>
        <code>A</code>
    </molecule>
    <molecule name='Lysine'>
        <symbol>lys</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 />';
}

As you see the above string is parsed using simplexml_load_string() function and the data are stored in the formed of array object of SimpleElement. After that, the array element is displayed by using foreach() loop of PHP.

Well, you might have already guessed the output of the parsed XML in browser. People who couldn’t guess the output don’t worry I’m a good guy and never leave the suspense in the middle. Here is the output of the code.

Alanine ala A
Lysine lys K

4 thoughts on “Parsing the XML in easy way using PHP

  1. PHP

    How to append another molecules information on above XML file through PHP?

  2. John A. Davis

    Yes! Now I have to wait until our new server comes and we install 5.0 on it. I think we are at 3.01 or something like that.

  3. PHP 3.01 ..oh god …. upgrade it dude…

  4. Sweet – I get it now.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>