I'm trying to create an itunes-valid podcast feed using php5's simplexml:

    <?php   
$xml_string = <<<XML <?xml version="1.0" encoding="UTF-8"?>
<channel> </channel> XML; $xml_generator = new SimpleXMLElement($xml_string); $tnsoundfile = $xml_generator->addChild('title', 'Main Title'); $tnsoundfile->addChild('itunes:author', "Author", ' '); $tnsoundfile->addChild('category', 'Audio Podcasts'); $tnsoundfile = $xml_generator->addChild('item'); $tnsoundfile->addChild('title', 'The track title');
$enclosure = $tnsoundfile->addChild('enclosure'); $enclosure->addAttribute('url', 'http://test.com'); $enclosure->addAttribute('length', 'filelength'); $enclosure->addAttribute('type', 'audio/mpeg');
$tnsoundfile->addChild('itunes:author', "Author", ' '); header("Content-Type: text/xml"); echo $xml_generator->asXML();
?>

It doesn't validate, because I've got to put the line:

   <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

as per http://www.apple.com/itunes/podcasts/specs.html.

So the output SHOULD be:

 <?xml version="1.0" encoding="UTF-8"?>
    <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
    <channel>

etc. I've been over and over the manual and forums, just can't get it right. If I put, near the footer:

   header("Content-Type: text/xml");
    echo '<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">';
    echo $xml_generator->asXML();
    ?>

Then it sort of looks right in firefox and it doesn't complain about undefined namespaces anymore, but feedvalidator complains that

line 1, column 77: XML parsing error: <unknown>:1:77: xml declaration not at start of external entity

because the document now starts:

    <rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"><?xml version="1.0" encoding="UTF-8"?>

and not

    <?xml version="1.0" encoding="UTF-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">

Any ideas gratefully received! Thank you.

    Well, the conclusion is that php simplexml isn't capable of producing an itunes-store-compliant podcast as specced at:
    http://www.apple.com/itunes/podcasts/specs.html

    So, here's the workaround:

    $xml_result = $xml_generator->asXML();
    $xml_LeftHandSide = '<?xml version="1.0" encoding="UTF-8"?><rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">';
    $xml_RightHandSide = substr($xml_result, 22);
    echo $xml_LeftHandSide . $xml_RightHandSide . "</rss>";

    et voila! feedvalidator and itunes are happy 🙂

    Wish I'd thought of this 5 nights ago...

      2 years later

      I had a similar problem two years later, but I found a much simpler solution. By simply adding the XML-namespaces to the XML-Code:

      $xml = new SimpleXMLElement('<Array xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />');
      $xml->addChild('Content', 'The Content');
      echo $xml->asXML();

      The output was:

      <Array xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
          <Content>The Content</Content>
      </Array>

      In your Code these lines would have been right:

      $xml_string = <<<XML
          <?xml version="1.0" encoding="UTF-8"?>
          [B]<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> [/B]
          <channel>
          </channel>
          [B]</rss>[/B]
          XML; 

      Important: don't forget the closing </rss>-tag at the end!
      Alternatively you could add <channel></channel> with

      $channel = $xml_generator->addChild('channel');
        Write a Reply...