I\'ve consistently had an issue with parsing XML with PHP and not really found \"the right way\" or at least a standardised way of parsing XML files.
Firstly i\'m tr
<?php
#Convert the String Into XML
$xml = new SimpleXMLElement($_POST['name']);
#Itterate through the XML for the data
$values = "VALUES('' , ";
foreach($xml->item as $item)
{
//you now have access to that aitem
}
?>
Media:content attributes are actually pretty easy to get with SIMPLE XML
if(!@$x=simplexml_load_file($feed_url)){
}
else
{
foreach($x->channel->item as $entry)
{
$media = $entry->children('http://search.yahoo.com/mrss/')->attributes();
$url = (string) $media['url'];
}
}
This was how i have eventually done it using XMLReader:
<?php
define ('XMLFILE', 'http://ws.audioscrobbler.com/2.0/artist/vasco%20rossi/images.rss');
echo "<pre>";
$items = array ();
$i = 0;
$xmlReader = new XMLReader();
$xmlReader->open(XMLFILE, null, LIBXML_NOBLANKS);
$isParserActive = false;
$simpleNodeTypes = array ("title", "description", "media:title", "link", "author", "pubDate", "guid");
while ($xmlReader->read ())
{
$nodeType = $xmlReader->nodeType;
// Only deal with Beginning/Ending Tags
if ($nodeType != XMLReader::ELEMENT && $nodeType != XMLReader::END_ELEMENT) { continue; }
else if ($xmlReader->name == "item") {
if (($nodeType == XMLReader::END_ELEMENT) && $isParserActive) { $i++; }
$isParserActive = ($nodeType != XMLReader::END_ELEMENT);
}
if (!$isParserActive || $nodeType == XMLReader::END_ELEMENT) { continue; }
$name = $xmlReader->name;
if (in_array ($name, $simpleNodeTypes)) {
// Skip to the text node
$xmlReader->read ();
$items[$i][$name] = $xmlReader->value;
} else if ($name == "media:thumbnail") {
$items[$i]['media:thumbnail'] = array (
"url" => $xmlReader->getAttribute("url"),
"width" => $xmlReader->getAttribute("width"),
"height" => $xmlReader->getAttribute("height"),
"type" => $xmlReader->getAttribute("type")
);
} else if ($name == "media:content") {
$items[$i]['media:content'] = array (
"url" => $xmlReader->getAttribute("url"),
"width" => $xmlReader->getAttribute("width"),
"height" => $xmlReader->getAttribute("height"),
"filesize" => $xmlReader->getAttribute("fileSize"),
"expression" => $xmlReader->getAttribute("expression")
);
}
}
print_r($items);
echo "</pre>";
?>
You may get the error Call to a member function getAttribute() on a non-object
if a feed is missing entries like thumbnail
, so while I like @Helder Robalo's answer you should check to make sure a node exists before trying to use things like getAttribute()
:
<?php
header('Content-type: text/plain; charset=utf-8');
$doc = new DOMDocument();
$doc->load('http://ws.audioscrobbler.com/2.0/artist/beatles/images.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
if( sizeof($node->getElementsByTagName('thumbnail')->item(0)) > 0 )
{
$itemRSS['thumbnail'] = $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url');
}
else
{
$itemRSS['thumbnail'] = '';
}
array_push($arrFeeds, $itemRSS);
}
print_r($arrFeeds);
Try this. It'll work fine.
$doc = new DOMDocument();
$doc->load('http://ws.audioscrobbler.com/2.0/artist/beatles/images.rss');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
'thumbnail' => $node->getElementsByTagName('thumbnail')->item(0)->getAttribute('url')
);
array_push($arrFeeds, $itemRSS);
}
Try using SimpleXML: http://us2.php.net/simplexml