Please bear with me. I'm a novice, but I really need to resolve this problem today. Thanks!
I'm creating an XML file via PHP using the following code:
PHP Code:
$playlist = array();
$playlist [] = array(
'creator' => 'Example Creator',
'title' => 'Example Title',
'location' => 'http://example.com/example.flv'
);
$playlist [] = array(
'creator' => 'Example Creator 2',
'title' => 'Example Title 2',
'location' => 'http://example.com/example2.flv'
);
$doc = new DOMDocument("1.0");
$doc->formatOutput = true;
// create root element
$p = $doc->createElement( "playlist" );
$doc->appendChild( $p );
// create child element
$t = $doc->createElement("tracklist");
$p->appendChild($t);
foreach( $playlist as $track )
{
$b = $doc->createElement( "track" );
$creator = $doc->createElement( "creator" );
$creator->appendChild(
$doc->createTextNode( $track['creator'] )
);
$b->appendChild( $creator );
$title = $doc->createElement( "title" );
$title->appendChild(
$doc->createTextNode( $track['title'] )
);
$b->appendChild( $title );
$location = $doc->createElement( "location" );
$location->appendChild(
$doc->createTextNode( $track['location'] )
);
$b->appendChild( $location );
$t->appendChild( $b );
$p->appendChild( $t );
}
$doc->save("write.xml");
Despite my best efforts I can't seem to get the XML to output exactly what I need, which is this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>
<track>
<creator>Example Creator</creator>
<title>Example Title</title>
<location>http://example.com/example.flv</location>
</track>
<track>
<creator>Example Creator</creator>
<title>Example Title</title>
<location>http://example.com/example2.flv</location>
</track>
</trackList>
</playlist>
My code isn't writing the encoding (encoding="UTF-8") or the namespace (xmlns="http://xspf.org/ns/0/").
How can I add those attributes to the corresponding nodes?
Thanks for your help!
Bookmarks