Jump to content

simplexml


skaterdav85

Recommended Posts

Im using simplexml to pull an rss feed. One of the xml tags however is <itunes:subtitle>, so if i try

$item->itunes:subtitle

in my foreach loop, PHP throws a syntax error. Anyone know how to get around this? I already tried using an escape character like this:

$item->itunes\:subtitle

but it didnt work.

Link to comment
Share on other sites

This is a namespaced node. You'll have to reach it with the children() method, by suppliying the namespace URI or prefix with the method's optional arguments.

Link to comment
Share on other sites

  • 2 weeks later...
This is a namespaced node. You'll have to reach it with the children() method, by suppliying the namespace URI or prefix with the method's optional arguments.
I think you can only use the children method if you're getting the attribute of a tag, kind of like the examples on the link you sent me. My XML looks like this:
<channel>  <item> 	 <itunes:author>Meghan McCoy</itunes:author> 	 <itunes:subtitle>some subtitle</itunes:subtitle>  </item></channel>

this is my php, but it doesnt work. What did I do wrong?

foreach($myXML->channel->item as $item) {	echo "Subtitle: ".$item->children('itunes:subtitle')."<br/>";}

Link to comment
Share on other sites

Look carefully at the description of the children() method. You need to supply information for the namespace, not for the element you're looking for. You need to say

$item->children('itunes', true)

or (what I'd reccomend you use)

$item->children('http://www.itunes.com/dtds/podcast-1.0.dtd')

Either of those is only going to get a collection of all elements in that namespace that are child of the current $item. You need to traverse this collection, and extract the information you need from it.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...