Search code examples
phpxml

php parse xml string


Possible Duplicate:
Best XML Parser for PHP

I have a string with XML data in it. How do I parse it in PHP?

thank you


Solution

  • Try with simple XML, here's an example:

    do.php:

    <?php
    $xml_str = file_get_contents('xmlfile.xml');
    $xml = new SimpleXMLElement($xml_str);
    $items = $xml->xpath('*/item');
    
    foreach($items as $item) {
        echo $item['title'], ': ', $item['description'], "\n";
    }
    

    xmlfile.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <xml>
        <items>
            <item title="Hello World" description="Hellowing the world.." />
            <item title="Hello People" description="greeting people.." />
        </items>
    </xml>