如何设置数组使PHP正确写入xml文件

时间:2013-05-05 00:09:02

标签: php xml api href

我陷入困境,试图弄清楚要为.php添加什么代码我的问题是xml文件的数组应该是什么,特别是“href links”

       $xml = new DOMDocument ("1.0","UTF-8");

       $playlists = $xml -> createElement("playlists");
       $playlists = $xml -> appendChild($playlists);

       $playlist = $xml ->  createElement ("playlist"); 
       $playlist = $playlists -> appendChild ($playlist);

       $track = $xml ->  createElement ("track");
       $track = $playlist -> appendChild ($track);

       $meta = $xml ->  createElement ("meta"); 
       $meta = $track -> appendChild ($meta);

       $xml->formatOutput = true;
       $string_value = $xml->saveXML();
       $xml->save("preview.xml");

XML-CODE:

<playlists>
    <playlist id="pl1">
        <track href="music/adg3com_bustedchump.mp3" title="Artist 1 - Track 1"     target="http://google.de" rel="covers/cover1.jpg">
            <meta>
                <![CDATA[ <a href="http://google.de" target="_blank">This is an optional text with a link</a> ]]>
            </meta>
        </track>
        <track href="music/adg3com_chuckedknuckles.mp3" title="Artist 2 - Track 2" target="" rel="">
            <meta><![CDATA[This is an optional text]]></meta>
        </track>
    </playlist>
    <playlist id="pl2">
        <track href="http://soundcloud.com/djlewisvi/afrojack-lionheart" title="Afrojack - Lionheart" />
        <track href="music/adg3com_chuckedknuckles.mp3" title="Artist 3 - Track 3" target="" rel="covers/cover1.jpg">
            <meta><![CDATA[This is an optional text]]></meta>
        </track>
        <track href="http://soundcloud.com/radykal/favorites" title="Soundcloud Favorites" />
    </playlist>
</playlists>

1 个答案:

答案 0 :(得分:0)

不确定我是否收到你的问题。如果它不适合,请让我知道并改进你的问题!
这是在href节点中创建<track>属性的方法:

$track = $xml ->  createElement ("track");
$href = $xml->createAttribute('href');
$href->value="http://soundcloud.com/djlewisvi/afrojack-lionheart";
$track->appendChild($href);
$track = $playlist -> appendChild ($track);

看到它有效:http://codepad.viper-7.com/uC9J2Y