一个最基本的PHP解析XML的实例

大牛勿喷 我等菜渣


php

<html>

  <head>

    <title>xmldemo</title>

  </head>

  <body>   

    <p>Welcome to the pop music home</p>
    <hr />
    <p>
    <ol>
      <li>This table will list the most popular music in this week.</li>
      <li>Also, my blog URL is <a href=>"http://blog.youkuaiyun.com/xboxmicro"</a></li>
      <li>This is a simple test about xml. If you have any questions, please feel free to contact with me.</li>
    </ol>
    <br />

    <?php
      $xmlfile = "interface.xml";
      $xml = new DOMDocument('1.0', 'utf-8');
      $dom->formatOutput = true;
      $xml->load($xmlfile);
      $root = $xml->documentElement;
      $nodes = $root->getElementsByTagName("song");
      $lengths = $nodes->length;
 
      $songlist = array();
      foreach($nodes as $val)
      {
        $tmp = array();
        $tmp[0] = $val->attributes->item(0)->nodeValue;
        $tmp[1] = $val->attributes->item(1)->nodeValue;
        $tmp[2] = $val->attributes->item(2)->nodeValue;
        $songlist[] = $tmp;
      }
      unset($xml);
    ?>

    <form id = "form1" name = "form1" method = "post" action = "">
      <table width = "713" border = "0" cellpadding = "0" cellspacing=  "1" bgcolor = "#000000">
        <tr>
          <td width = "350" height = "25" align = "center" bgcolor = "#CCCCCC">歌曲路径</td>
          <td width = "499" height = "25" align = "center" bgcolor = "#CCCCCC">歌曲名称</td>
          <td width = "499" height = "25" align = "center" bgcolor = "#CCCCCC">歌曲排序</td>
        </tr>
    <?php
      foreach($songlist as $song)
      {
    ?>
    <tr>
      <td height = "25" bgcolor = "#FFFFFF">
        <input name = "path[]" type = "text" value = "<?php echo $song[0];?>" size = "50" /></td>
      <td height = "25" bgcolor = "#FFFFFF">
       <input name = "title[]" type = "text" value = "<?php echo $song[1];?>" size = "35" /></td>
      <td height = "25" bgcolor = "#FFFFFF">
        <input name = "plist[]" type = "text" value = "<?php echo $song[2];?>" size = "35" /></td>
    </tr>
    <?php
      }
    ?>
      </table>
    </form>
  </body>
</html>

XML

<?xml version="1.0" encoding="UTF-8"?>
<player showDisplay="yes" showPlaylist="yes" autoStart="yes">
<song path="music1.mp3" title="卷珠帘" plist="1"/>
<song path="music2.mp3" title="时间都去哪里了" plist="2"/>
<song path="music3.mp3" title="列车员style" plist="3"/>
</player>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值