Using PHP SimpleXML to get XML Namespace Elements
by Sherif
PHP has a great SimpleXML library that converts XML to an object that can be processed with normal property selectors and array iterators. I’ve been using this quite a bit lately to process some XML documents.
The library documentation isn’t that great when it comes to processing Namespace Elements within your XML document. An example of such use case is when you are parsing an RSS feed that has XML Namespace elements.
Consider the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:dc="http://purl.org/dc/elements/1.1/"> <channel> .... <item> <title>My Title</title> <description>My Item</description> <dc:publisher>ABC</dc:publisher> <dc:creator>DEF</dc:creator> <dc:date>2009-02-12T16:53:25Z</dc:date> </item> ... </channel> </rss> |
For me to access things like the Title and Description elements, its as simple as:
1 2 3 4 5 6 | $feed = file_get_contents("http://linkto.my.feed"); $xml = new SimpleXmlElement($feed); foreach ($xml->channel->item as $entry){ echo $entry->title; echo $entry->description; } |
But what if I want to access my namespace elements such as dc:publisher or dc:creator? You would think it ‘could’ be as simple as this:
1 2 3 4 5 6 7 | //This doesn't work ... foreach ($xml->channel->item as $entry){ echo $entry->publisher; echo $entry->creator; ... } |
The code above doesn’t work because the publisher and creator elements sit inside different namespaces. So how do we do this? If you recall, the second line of our feed had this:
1 | .... xmlns:dc="http://purl.org/dc/elements/1.1/"> |
So we know from above that anything in the dc namespace refers this URL: http://purl.org/dc/elements/1.1. Now that we know this, we can easily do this:
1 2 3 4 5 6 7 8 9 10 | $feed = file_get_contents("http://linkto.my.feed"); $xml = new SimpleXmlElement($feed); foreach ($xml->channel->item as $entry){ echo $entry->title; echo $entry->description; //Use that namespace $dc = $entry->children(‘http://purl.org/dc/elements/1.1/’); echo $dc->publisher; echo $dc->creator; } |
That would work. Now a cleaner way is to read the namespace URI form the document itself using the getNamespaces method:
1 2 3 4 5 6 7 8 9 10 | ... foreach ($xml->channel->item as $entry){ ... //Use that namespace $namespaces = $entry->getNameSpaces(true); //Now we don't have the URL hard-coded $dc = $entry->children($namespaces['dc']); echo $dc->publisher; echo $dc->creator; } |
That’s it! I found this useful when getting an RSS feed using SimpleXML and wanting to parse the XML Namespace elements.
[...] Bookmarked Using PHP SimpleXML to get XML Namespace Elements : Blog the web [...]
Thanks for the namespace trick that’s exactly what I was looking for!
thanksss so much
Thank you very much, your examples finally helped me with my parsing problems.
Thanks for this, super helpful.
Many thanks, that helped me a lot too!
Good stuff!
VERY helpful. Thank you!
There are several ways to read RSS feed in PHP, but this one is surely one of the easiest.
channel->item as $entry) {
echo “link’ title=’$entry->title’>” . $entry->title . “”;
}
?>
Source:
http://phphelp.co/2012/04/23/how-to-read-rss-feed-in-php/
OR
http://addr.pk/a0401
thx bro, this helped me a lot
[...] a useful blog article here explaining how to access namespaced elements and attributes using simpleXML, that should provide [...]
hey thnx for this , if i wan to access namespace in a attribute
heello
how to access gd:etag in link , thnx in advance
thnks(:
Really thanks to you!!
I’ve spent so much time with it before I’ve found your article.
hey,
Im a newbie at this, but I have the xml file for this project I’m doing and I need to convert it to php .
Thank you very much…. It solved my long term problem…
I believe this is very close to what I need, but in the XML file I’m trying to parse, the namespaces exist in the root element instead of in the child elements. How can I then parse the child elements from a namespaced root element?
Hi, how i can retrive an attribute on namespaced element? i have tried with $dc->publisher['attribute'] but seems to not function, thanks in advance.
This helped me out
Thank you for your help. Good explanation and very convenient
Great help. This works!
My man! Thanks.
Big thanks for your article, very helpful !!!
Thank you so much. I didn’t think this work, after trying 10 different methods to parse XML.
I love you.