一個最基本的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.csdn.net/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>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章