dptj13337 2012-02-27 00:29
浏览 25
已采纳

用php更改xml属性

Im trying to create a mp3 player that will add songs per day. So after long search over the net for months. Finally came up with an option, a flash player with a xml file which is controlled by a PHP code determining the date. since this is the first project im doing in PHP im totally lost.

Here is the xml for the flash player which I have downloaded

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <param name="mp3" value="tracks/2.mp3|tracks/1.mp3" />
    <param name="title" value="Day 2|Day 1" />
    <param name="height" value="150" />
    <param name="width" value="250" />
    .........

The first two children's "value" attribute should change per day.

And here is the PHP code I have come up with which doesn't work

$data = simplexml_load_file('tracks/list.xml');
$xml = new SimpleXMLElement($data);

foreach($xml->param[0]->attributes() as $name => $value)
foreach($xml->param[1]->attributes() as $name2 => $value2)

$today=date(d);
if($today==01){
    $value.setAttribute("tracks/1.mp3");
    $value2.setAttribute("Day 1");
    echo $xml->saveXML();
}
if($today==02){
    $value.setAttribute("tracks/2.mp3|tracks/1.mp3");
    $value2.setAttribute("Day 2|Day 1");
    echo $xml->saveXML();
}
if($today==03){
    $value.setAttribute("tracks/3.mp3|tracks/2.mp3|tracks/1.mp3");
    $value2.setAttribute("Day 3|Day 2|Day 1");
    echo $xml->saveXML();
}
.........

Please help me out on how to fix this. Thanks

  • 写回答

2条回答 默认 最新

  • doukuipei9938 2012-02-27 12:23
    关注

    You can use XQuery to do the job:

    let $config := 
    <config>
        <param name="mp3" value="tracks/2.mp3|tracks/1.mp3" />
        <param name="title" value="Day 2|Day 1" />
    </config>
    let $today := day-from-date(current-date())
    return
        switch(1)
        case 1 return {
            replace value of node $config/param[1]/@value with "tracks/1.mp3";
            replace value of node $config/param[2]/@value with "Day 1";
            $config
        }
        default return ()
    

    You can try the following example live at http://www.zorba-xquery.com/html/demo#bYJOffXahl2PVhczDk+EgmZ1bAI= Instructions on how to install the XQuery PHP extension are available at http://www.zorba-xquery.com/html/entry/2011/12/27/PHP_Meets_XQuery

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并