I am trying to parse an RSS file using the following PHP:
$doc = new DOMDocument();
$doc->load($address);
$feedArr = array();
foreach ($doc->getElementsByTagName('item') as $node) {
$itemRSS = array (
'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue
);
foreach($node->getElementsByTagName('category') as $catNode) {
$itemRSS['category'][] = $catNode->nodeValue;
}
}
It works fine except if the category node has a domain attribute. When it does, it seems to completely ignore it the category.
Any ideas why?
You can access XML attributes like this:
'whatever' => $node->getElementsByTagName('node')->getAttribute('category')
So I presume your should just amend your loop to this:
foreach($node->getElementsByTagName('node')->getAttribute('category') as $catNode) {
}
Although I may be a bit off on the syntax as I haven't coded in PHP for a while, although getAttribute is definitely what you are wanting.
Actually, there is a good tutorial here using Simplexml function in PHP: http://webtutsdepot.com/2009/07/26/how-to-parse-xml-with-php-part2-xml-with-atrributes/