It’s a little bit tricky to parse and use XML data when tags have different namespaces. We would have to use the namespace string when accessing the objects like below with $dc_namespaces
:
XML with two different namespaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<!-- 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 code to access the parsed object:
1
2
3
4
5
6
7
8
9
|
<?php
$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;
}
|