Home >Backend Development >PHP Tutorial >How Can I Parse XML Nodes with Colons Using SimpleXML?

How Can I Parse XML Nodes with Colons Using SimpleXML?

Barbara Streisand
Barbara StreisandOriginal
2024-12-29 06:50:10653browse

How Can I Parse XML Nodes with Colons Using SimpleXML?

Parsing XML Nodes with Colons Using Simple XML

Accessing XML elements containing colons (:) in their names can pose a challenge when using the SimpleXML extension. Nodes like "media:thumbnail" and "flickr:profile" are often encountered in feeds such as Flickr's RSS, hindering straightforward retrieval.

To navigate this obstacle without resorting to the DOM, one can employ the children() method of SimpleXML. This method allows for accessing elements by providing a namespace URI.

For instance, to retrieve the thumbnail from a Flickr RSS feed, one can utilize the following code:

$feed = simplexml_load_file('http://www.sitepoint.com/recent.rdf'); 
foreach ($feed->item as $item) { 
    $ns_dc = $item->children('http://purl.org/dc/elements/1.1/'); 
    echo $ns_dc->date; 
}

The above is the detailed content of How Can I Parse XML Nodes with Colons Using SimpleXML?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn