Jump to content

Archived

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

pkrtech

Reading XML NAMESPACE using DOM

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;
    }
}

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[code]<?php
$nodelist = $xdom->getElementsByTagName("media:description");
foreach ($nodelist as $element){
echo $element->nodeValue.'<br />';
}
?>[/code]

Share this post


Link to post
Share on other sites
Just noticed your modification.

Try this instead:
[code]<?php
$nodelist = $xdom->getElementsByTagNameNS('media','description');
?>[/code]

Share this post


Link to post
Share on other sites
Well, getElementsByTagNameNS shouldn't work (and it doesn't dissapoint in that). getElementsByTagName('NS:TAG') should work as far as
I can tell, but I can't seem to get it either.  ???

Share this post


Link to post
Share on other sites
[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...

Share this post


Link to post
Share on other sites
Wow you rock! Thanks so much. You dont know what I been going through to get that. Karma is yours today my friend.

Share this post


Link to post
Share on other sites

×

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.