Parsing the XML in easy way using PHP

Advertisement

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

Popularity: 5% [?]

Tags: ,

Enter your email address and get free tutorials, tips and tricks of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Related Posts

» Clean and efficient coding technique in PHP
» Uploading large(big) files in PHP using .htaccess
» Create mobile sites in a fly using mobilemo
» Blogrush is Racist – It sucks

3 Comments on “Parsing the XML in easy way using PHP”

  • PHP wrote on 25 June, 2008, 11:00

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

  • John A. Davis wrote on 27 June, 2008, 16:52

    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.

  • Roshan wrote on 28 June, 2008, 16:21

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

Write a Comment