Jump to content

DOMDocument XML declaration


burnthand

Recommended Posts

Hi there ppl

I'm using the DOMdocument object to interact with XML playlist files for a music archive site that we are updating/building to archive our many free form jam sessions over the last few years - go to www.merecoincidence.co.uk if you're interested in hearing some random impro.

 

The XML files need to have an XML declaration that reads

<playlist version="1" xmlns="http://xspf.org/ns/0/">

in order to interct with the media player we're using but i don't seem to be able to find a way to change the XML declaration other than to use:

$doc = new DOMDocument('1', 'xmlns="http://xspf.org/ns/0/"')

when newing a DOMdocument. but this only changes the attributes and not the actual node type, so its still returning

<?xml version="1.0"?>

 

I'm considering using file IO with regex's to get round the problem, but this is a bit of a dirty hack really.

 

Can I manipulate the XML Declaration of a DOMDocument so that it reads '<playlist' rather than '<?xml' or remove the declaration altogether? I can't seem to find a way.

Link to comment
Share on other sites

sorry for the late reply. This is the demo xml file that comes with the media player, so the track listings are irrelivent.

 

Desired xml output:

 

<playlist version="1" xmlns="http://xspf.org/ns/0/">
<trackList>

	<track>
		<title>Homeland</title>
		<creator>Postman</creator>
		<location>http://www.jeroenwijering.com/upload/postmen.mp3</location>
		<image>http://www.jeroenwijering.com/upload/postmen.jpg</image>

		<info><![CDATA[http://www.amazon.com/Revival-Postmen/dp/B00005J8R7/sr=1-6/qid=1166887655/ref=sr_1_6/105-9454024-2355627?ie=UTF8&s=music]]></info>
		<identifier>456</identifier>
	</track>

	<track>
		<title>Jazzalude</title>
		<creator>Basement Jaxx</creator>
		<location>http://www.jeroenwijering.com/upload/basement.mp3</location>

		<image>http://www.jeroenwijering.com/upload/basement.jpg</image>
		<identifier>123</identifier>
		<info><![CDATA[http://www.amazon.com/Rooty-Basement-Jaxx/dp/B00005K9V5/sr=8-3/qid=1166887603/ref=pd_bbs_sr_3/105-9454024-2355627?ie=UTF8&s=music]]></info>
	</track>


	<track>
		<title>Sunshine Up Ahead</title>

		<creator>Peter Jones</creator>
		<location>http://www.jeroenwijering.com/upload/peterjones.mp3</location>
		<image>http://www.jeroenwijering.com/upload/peterjones.jpg</image>
		<info>http://www.peterjonesmusic.net</info>
		<identifier>789</identifier>
	</track>


	<track>
		<title>Jazzalude</title>
		<creator>Basement Jaxx</creator>
		<location>http://www.jeroenwijering.com/upload/basement.mp3</location>
		<image>http://www.jeroenwijering.com/upload/basement.jpg</image>
		<info><![CDATA[http://www.amazon.com/Rooty-Basement-Jaxx/dp/B00005K9V5/sr=8-3/qid=1166887603/ref=pd_bbs_sr_3/105-9454024-2355627?ie=UTF8&s=music]]></info>

		<identifier>123</identifier>
	</track>

	<track>
		<title>Homeland</title>
		<creator>Postman</creator>
		<location>http://www.jeroenwijering.com/upload/postmen.mp3</location>

		<image>http://www.jeroenwijering.com/upload/postmen.jpg</image>
		<info><![CDATA[http://www.amazon.com/Revival-Postmen/dp/B00005J8R7/sr=1-6/qid=1166887655/ref=sr_1_6/105-9454024-2355627?ie=UTF8&s=music]]></info>
		<identifier>456</identifier>
	</track>

</trackList>

 

Actual XML output:

 

<?xml version="1.0"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<trackList>

	<track>
		<title>Homeland</title>
		<creator>Postman</creator>
		<location>http://www.jeroenwijering.com/upload/postmen.mp3</location>
		<image>http://www.jeroenwijering.com/upload/postmen.jpg</image>

		<info><![CDATA[http://www.amazon.com/Revival-Postmen/dp/B00005J8R7/sr=1-6/qid=1166887655/ref=sr_1_6/105-9454024-2355627?ie=UTF8&s=music]]></info>
		<identifier>456</identifier>
	</track>

	<track>
		<title>Jazzalude</title>
		<creator>Basement Jaxx</creator>
		<location>http://www.jeroenwijering.com/upload/basement.mp3</location>

		<image>http://www.jeroenwijering.com/upload/basement.jpg</image>
		<identifier>123</identifier>
		<info><![CDATA[http://www.amazon.com/Rooty-Basement-Jaxx/dp/B00005K9V5/sr=8-3/qid=1166887603/ref=pd_bbs_sr_3/105-9454024-2355627?ie=UTF8&s=music]]></info>
	</track>


	<track>
		<title>Sunshine Up Ahead</title>

		<creator>Peter Jones</creator>
		<location>http://www.jeroenwijering.com/upload/peterjones.mp3</location>
		<image>http://www.jeroenwijering.com/upload/peterjones.jpg</image>
		<info>http://www.peterjonesmusic.net</info>
		<identifier>789</identifier>
	</track>


</trackList>
</playlist>

 

Currently I have to add the </playlist> closing tag as well otherwise the DOMDocument won't parse the XML at all.

Link to comment
Share on other sites

Here are the functions for generating the playlists, they aren't working at the moment because the DOMDocument keeps adding an xml declaration and putting the nodes in the wrong place.

 


function addTrack( &$doc , &$track )
{
$r = $doc->documentElement;
$b = $doc->createElement( "track" );
$elements = array( 'title', 'creator', 'location', 'image', 'info', 'identifier' );

foreach( $elements as $element ){ 
	$createEl = $doc->createElement( $element );
	$createEl->appendChild( $doc->createTextNode( $track[$element] ));
	$b->appendChild( $createEl );
	}
	$r->appendChild( $b );

}

function updatePlaylist()
{

}

function createPlaylist( $path , $tracks )
{
$doc = new DOMDocument();
$doc->formatOutput = true;
foreach( $tracks as $track ){
	$b = $doc->createElement( "track" );
	$elements = array( 'title', 'creator', 'location', 'image', 'info', 'identifier' );
	foreach( $elements as $element ){
		$createEl = $doc->createElement( $element );
		$createEl->appendChild( $doc->createTextNode( $track[$element] )
			);
		$b->appendChild( $createEl );
	}
	$r->appendChild( $b );
}

echo $doc->save( $path . 'playlist.xml' );
}
// read each filename as an entry
				/*	while ( false !== ( $file = readdir( $newhandle ) ) ){
				// if the current entry is a . or a .. exclude it from the listing
				if ( $file != "." && $file != ".." ){

					echo "$file<br/>";
					array_push( $files, $file );
				}
			} */
?>

 

heres the output

 

<?xml version="1.0"?>
<playlist xmlns="http://xspf.org/ns/0/" version="1">
<trackList>

	<track>
		<title>Homeland</title>
		<creator>Postman</creator>
		<location>http://www.jeroenwijering.com/upload/postmen.mp3</location>
		<image>http://www.jeroenwijering.com/upload/postmen.jpg</image>

		<info><![CDATA[http://www.amazon.com/Revival-Postmen/dp/B00005J8R7/sr=1-6/qid=1166887655/ref=sr_1_6/105-9454024-2355627?ie=UTF8&s=music]]></info>
		<identifier>456</identifier>
	</track>

	<track>
		<title>Jazzalude</title>
		<creator>Basement Jaxx</creator>
		<location>http://www.jeroenwijering.com/upload/basement.mp3</location>

		<image>http://www.jeroenwijering.com/upload/basement.jpg</image>
		<identifier>123</identifier>
		<info><![CDATA[http://www.amazon.com/Rooty-Basement-Jaxx/dp/B00005K9V5/sr=8-3/qid=1166887603/ref=pd_bbs_sr_3/105-9454024-2355627?ie=UTF8&s=music]]></info>
	</track>


	<track>
		<title>Sunshine Up Ahead</title>

		<creator>Peter Jones</creator>
		<location>http://www.jeroenwijering.com/upload/peterjones.mp3</location>
		<image>http://www.jeroenwijering.com/upload/peterjones.jpg</image>
		<info>http://www.peterjonesmusic.net</info>
		<identifier>789</identifier>
	</track>


</trackList>
<track><title>test3</title><creator>HB</creator><location>./music/011207/</location><image></image><info></info><identifier></identifier></track></playlist>

 

 

Link to comment
Share on other sites

Well assuming your storing the document in a string before output,

 

it might be easy to just as well add before the output line:

 

$doc = str_replace("<?xml version="1.0"?>", "", $doc);

 

if the player don't like white spaces above the initial <playlist> tag

 

you can try

 

$doc = str_replace("<?xml version="1.0"?>\n", "", $doc);

 

(That is if all you need to do is remove the <?xml version="1.0"?> line)

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.