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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.