Jump to content

Reading XML NAMESPACE using DOM


pkrtech

Recommended Posts

Can someone tell me howto or why I cannot seem to grab the description from google video rss feed under element media:description.

Any Help appreciated THANKS!

Code:
$xdom = new domDocument;
$cycle = 'http://video.google.com/videofeed?type=search&q=is%3Aforsale&so=1&num=20&output=rss';
$xdom->load($cycle);

$channel = $xdom->getElementsByTagName("item");
foreach( $channel as $item )
  {
  $xdom->getElementsByTagName("item");
  $title = $item->getElementsByTagName( "title" );
  $titles = $title->item(0)->nodeValue;
  $titles = htmlentities($titles, ENT_QUOTES);

  // this will pull the crappy description that I dont want
  $descript = $item->getElementsByTagName("description");
  $description = $descript->item(0)->nodeValue;
  $description = htmlentities($description, ENT_QUOTES);

  //This should pull proper formatted description in media:description but doesnt

  $break = $xdom->getElementsByTagName("media");
   foreach($break as $media)
    {
    $break->getElementsByTagName("media");
    $descript2 = $media->getElementsByTagName("description");
    $description2 = $descript2->media(0)->nodeValue;
    }
}
Link to comment
https://forums.phpfreaks.com/topic/18452-reading-xml-namespace-using-dom/
Share on other sites

$xdom->getElementsByTagName("media:description");

"media" is the namespace. When specified, it needs to be included. I think. I know this to be true for fetching attribute nodes, so my guess is it also aplies to element nodes.

Edit for clarification:
You're trying to access 'description' as it where a child element of 'media', it's not. Like I said, 'media' refers to the namespace.
[code]<?php
$xdom = new DOMDocument;
$cycle = 'http://video.google.com/videofeed?type=search&q=is%3Aforsale&so=1&num=20&output=rss';
$xdom->load($cycle);
$items = $xdom->getElementsByTagName("item");
foreach($items as $item){
$nodelist = $item->getElementsByTagName('description');
foreach ($nodelist as $element){
if($element->tagName == 'media:description'){
echo htmlspecialchars($element->nodeValue).'<br />';
}
}
}
?>[/code]

Above works. It amazes me though, why getElementsByTagName doesn't take the namespace, while the DOMElement->tagName property does include it ??? Very very strange...

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.