在文档中心查找“使用PHP DOM-XML创建和解析XML文件”。

解决方案 »

  1.   

    <?
    class myxml
    {
    var $parser;

    function myxml()
    {
    $this->parser = xml_parser_create();
    xml_set_object($this->parser,&$this);
    xml_set_element_handler($this->parser, "tag_on", "tag_off");
    xml_set_character_data_handler($this->parser, "mycdata");
    }

    function tag_on($parser, $tag, $attributes)
    {
    $curr_line = xml_get_current_line_number($parser);
    echo "<font color=red>parser=".$parser.";tag=".$tag.";attributes=".$attributes.";curr_line=".$curr_line."</font><br>";
    if($curr_line==2)
    {
    echo "\n<tr>";
    }
    else
    {
    if($curr_line!=1)
    {
    $value = ($curr_line-3)%6;
    echo "<font color=green>value=".$value."</font><br>";
    switch((($curr_line-3)%6))
    {
    case 0:
    echo "\n<tr>";
    break;
    case 1:
    echo "\n<td>";
    break;
    case 2:
    echo "\n<td>";
    break;
    case 3:
    echo "\n<td>";
    break;
    case 4:
    echo "\n<td>";
    break;
    case 5:
    break;
    }
    }
    }
    }

    function tag_off($parser, $tag)
    {
    $curr_line = xml_get_current_line_number($parser);
    if($curr_line!=1 && $curr_line!=2)
    {
    switch((($curr_line-3)%6))
    {
    case 0:
    break;
    case 1:
    echo "</td>";
    break;
    case 2:
    echo "</td>";
    break;
    case 3:
    echo "</td>";
    break;
    case 4:
    echo "</td>";
    break;
    case 5:
    echo "\n</tr>";
    break;
    }
    }
    }

    function mycdata($parser, $cdata)
    {
    echo $cdata;
    }

    function parse($data)
    {
    xml_parse($this->parser,$data);
    }
    }$xml_parser = new myxml();
    $xmlfilename = "students.xml";
    $fp = fopen($xmlfilename, "r");
    $xmldata = fread($fp, filesize($xmlfilename));echo "<table border=1 width=100%>";
    echo "<tr>";
    echo "<td>id</td>";
    echo "<td>name</td>";
    echo "<td>password</td>";
    echo "<td>test</td>";
    echo "</tr>";
    $xml_parser->parse($xmldata);
    echo "</table>";
    ?>
      

  2.   

    students.xml文件如下<?xml version="1.0" encoding="GB2312" ?>
    <USERMANAGEMENT>
    <students>
    <student>
    <id>1</id>
    <name>aa</name>
    <password>password</password>
    <test>test</test>
    </student>
    <student>
    <id>2</id>
    <name>bb</name>
    <password>password</password>
    <test>test</test>
    </student>
    <student>
    <id>3</id>
    <name>cc</name>
    <password>password</password>
    <test>test</test>
    </student>
    </students>
    </USERMANAGEMENT>
      

  3.   

    本人觉得如果可以对浏览器作些限制(ie5以上),可以直接用xml和html结合的方法完成页面的显示,方法如下:<XML id='studentsinfo' src='aa.xml'>";
    </XML><table border=1 width=100% id='studentslist' datapagesize='20' datasrc='#studentsinfo'>
    <thead>
    <tr>
    <td>id</td>
    <td>name</td>
    <td>password</td>
    </tr>
    <tbody>
    <tr>
    <td><span datafld='id'></span></td>
    <td><span datafld='name'></span></td>
    <td><span datafld='password'></span></td>
    </tr>
    <tfoot>
    <tr>
                <td width="600" colspan="3">
                    <table cellpadding="0" cellspacing="0">
                        <tr>
                            <td>
                                <img src="first.gif" onclick="languagelist.firstPage()">
                            </td>
                            <td>
                                <img src="back.gif" onclick="languagelist.previousPage()">
                            </td>
                            <td>
                                <img src="next.gif" onclick="languagelist.nextPage()">
                            </td>
                            <td>
                                <img src="last.gif" onclick="languagelist.lastPage()">
                            </td>
                        </tr>
                    </table>
                </td>
    </tr>
    </table>