Search code examples
phpxmlxml-parsingjwplayer

Php write XML file (to JW player)


I use the JW player to load a XML playlist. It works fine when I manually write the XML file, but not when I use php to parse...

I want it to look like this:

<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:jwplayer="http://developer.longtailvideo.com/trac/"> 
<channel>
  <item>
    <title>Albert</title>
    <media:content url="../movies/hi.mp4" />
    <description></description>
    <jwplayer:duration>10</jwplayer:duration>
  </item>
</channel>
</rss>

The first problem is the <rss version="2.0" ... It forces the headers to be: <?xml version="1.0"?>

The second problem is the <media:content url="" ... How can I print out that with php ?

The third problem is how to add the end rss </rss>

My code is:

<?php 
  $channel = array(); 
  $channel [] = array( 
  'title' => 'Albert', 
  'content' => 'filmer/c1.jpg', 
  'duration' => "10" 
  ); 
  $channel [] = array( 
  'title' => 'Claud', 
  'content' => 'filmer/c2.jpg', 
  'duration' => "10" 
  ); 

  $doc = new DOMDocument();
  $doc->formatOutput = true; 

  $r = $doc->createElement( "channel" ); 
  $doc->appendChild( $r ); 

  foreach( $channel as $item ) 
  { 
  $b = $doc->createElement( "item" ); 

  $title = $doc->createElement( "title" ); 
  $title->appendChild( 
  $doc->createTextNode( $item['title'] ) 
  ); 
  $b->appendChild( $title ); 

  $content = $doc->createElement( "media:content" ); 
  $content->appendChild( 
  $doc->createTextNode( $item['content'] ) 
  ); 
  $b->appendChild( $content ); 

  $duration = $doc->createElement( "jwplayer:duration" ); 
  $duration->appendChild( 
  $doc->createTextNode( $item['duration'] ) 
  ); 
  $b->appendChild( $duration ); 

  $r->appendChild( $b ); 
  } 

  echo $doc->saveHTML(); 
  $doc->save("write.xml") 
  ?>

Any ideas? I'm a newbie in PHP/XML, sorry :/


Solution

  • This line: <?xml version="1.0"?> is called XML Declaration and it is optional. So whether that line is there or not should not make any difference and pose any problems as long as you use valid XML.

    As RSS is based on XML, you do not need to worry about that line being there.

    I hope this clarifies this part of your question.

    And as Q&A normally works best with one question each, here are those other two: