Search code examples
phpxmlxml-parsingsimple-html-domxspf

Extract .XSPF contents


I have a XSPX file containing some data, its structure is

<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/" xmlns:vlc="http://www.videolan.org/vlc/playlist/ns/0/">

    <trackList>

            <track>
                <album></album>
                <title>Welcome Song</title>
                <location>http://mysite.com/song.mp3</location>

                    <duration>20000</duration>

                <info>Information about the song</info>



            </track>

    </trackList>
</playlist>

I tried to extract these data with PHP but it gives me errors, sometimes nothing.

$data = new DOMDocument(); $data->load('http://full/path/to/tracklist.xspf'); 

if($data->load("http://full/path/to/tracklist.xspf")) { 
    foreach ($data->getElementsByTagName('trackList') as $track) { 
        $title = $track->getElementsByTagName('title')->item(0)->nodeValue; 
        $info = $track->getElementsByTagNameNS('info')->item(0)->nodeValue; 
        print($track); 
    } 
  }

Where am i doing wrong?


Solution

  • I have seen several questions same as mine up on the internet but with no accepted answers.

    At first I didn't realize .XSPF file data structure is same as XML, so you can just load the .XSPF file instead of .XML into a document and do parsing.

    Cheers!