Generating Podcast RSS using PHP5 Dom

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

Generating Podcast RSS using PHP5 Dom

Post by ravervx »

Hi All,

This is my first time in a long time posting on the forum. I think this forum is great.

I'm currently working on a RSS/Podcast generating library using php5's dom library. I'm running into a little problem with xml namespace.

First, I'm not generating the correct namespace string. Below is my code setting the namespace for itunes podcast.

Code: Select all

 
$qualifiedName = 'itunes';
$namespaceURI = 'http://www.itunes.com/dtds/podcast-1.0.dtd';
 
public function setXmlNameSpace($namespaceURI, $qualifiedName)
  {
    $this->nameSpaceURI = $namespaceURI;
    
    $this->qualifiedName = $qualifiedName;
    
    $attrNS = $this->getXMLDocument()->createAttributeNS($namespaceURI, $qualifiedName);
    
    $this->getRootElement()->appendChild($attrNS);
  }
 
The above code generated this:

Code: Select all

 
<rss xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd" itunes="" version="2.0">
 
While I want it like this:

Code: Select all

 
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
 
I'm also having trouble with Xpath to search for nodes. Below is my getElements function

Code: Select all

 
  public function getElements($xpathQuery)
  {    
    $xpath = new DOMXPath($this->getXMLDocument());
    
    $xpath->registerNamespace($this->qualifiedName, $this->nameSpaceURI);
    
    $domNodeList = $xpath->query($xpathQuery, $this->getXMLDocument());
    
    if($domNodeList->length > 0)
    {
      return $domNodeList;
    }
 
    return false;
  }
 
Here is an example query and xml:

Code: Select all

 
qualifiedName: itunes
 
nameSpaceURI: http://www.itunes.com/dtds/podcast-1.0.dtd
 
xpathQuery: itunes:title
 
domNodeList->length: 0
 
XML: 
 <?xml version="1.0" encoding="utf-8"?>
<channel xmlns="http://www.itunes.com/dtds/podcast-1.0.dtd" itunes="">
  <title>Podcasts Feed</title>
  <link>http://www.localhost.local/podcasts.htm</link>
  <language>en-us</language>
  <copyright>&copy;2008 xxxx.</copyright>
  <itunes:author>localhost</itunes:author>
  <itunes:name>John Doe</itunes:name>
  <itunes:email>localhost@localhost.com</itunes:email>
  <itunes:image href="http://www.localhost.local/images/site/logo.jpg" />
</channel>
 
I have been reading all the dom documentations, but they are not that helpful with no examples on usage.
Any one can give me a bit of advise is appreciated. I have been trying to solve this problem for the past 2 days.

Thanks

Guang
Post Reply