Page 1 of 1

Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 12:00 pm
by patrickdaly
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:

Code: Select all

$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: Select all

<?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! :D

Re: Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 2:09 pm
by requinix
For one, you're using ->save() which is supposed to be for HTML files. Try using ->saveXML() instead.
For another, createElement doesn't know about namespaces. Use createElementNS on the root to set the default namespace for all the other nodes.

Re: Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 4:01 pm
by patrickdaly
Thanks for the help so far!

I made a couple changes based on your advice and I'm further along, but not there yet...

In the previous code, lines 16-18, where I'm creating the root element, I replace it with the following code:

Code: Select all

 // create root element
$p = $doc->createElementNS( "http://xspf.org/ns/0/","playlist" );
$doc->appendChild( $p ); 
Now I'm getting the namespace to show up. Now I need to add the version attribute to the same line so that the XML output should be:

Code: Select all

<playlist version="1" xmlns="http://xspf.org/ns/0/">
I've tried my hand with createAttritbute(), but just can't get it. Remember, I'm a novice!

Re: Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 4:38 pm
by invertrio
I think the method you're looking for is setAttribute().

Code: Select all

$p->setAttribute( 'version', '1' );

Re: Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 4:47 pm
by patrickdaly
That did it! Thanks.

I think I only have one last question.

How can I set the encoding?

I figured that it could be somewhat similarly to the version number like in the following. I read somewhere that it can't be done that way, but didn't find a solution.

Code: Select all

$doc = new DOMDocument("1.0");
I need the UTF-8 encoding like so:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>

Re: Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 5:03 pm
by requinix
DOMDocument's constructor can take two arguments. Guess what the second one is.

Re: Dynamically Creating an XML File - Problems

Posted: Wed Dec 10, 2008 5:12 pm
by patrickdaly
Thanks! I guess I should have tried it anyway...I just read that it only took one argument somewhere else.

Thanks for everyone's help!