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
Release: 2024-12-29 06:50:10
Original
645 people have browsed it

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; 
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template