сряда, септември 01, 2010

SimpleXML parse elements with different namespace

Example XML file with two different namespaces (xmldata.xml):
<!-- xmldata.xml -->
<rootnode xmlns="http://mydomain.com/2010">
    <node>
        <title>This is the first Node</title>
        <meta>
            <dc:language>en</dc:language>
        </meta>
    </node>
    <node>
        <title>This is the second Node</title>
        <meta>
            <dc:language>en</dc:language>
        </meta>
    </node>
</rootnode>

PHP script to get the data:
$dc_namespace = 'http://purl.org/dc/elements/1.1/';

$data = simplexml_load_file('xmldata.xml');
foreach($data as $node)
{
    $title = $node->title;
    $lang  = $node->meta->children($dc_namespace)->language;
}

Няма коментари: