【问题标题】:Reading Data from XML to HTML Table从 XML 读取数据到 HTML 表
【发布时间】:2017-05-20 01:22:26
【问题描述】:

您好,我正在尝试将 XML 文件中的记录读取到我的 HTML 表中。我试图实现的代码不起作用,只显示空白页。你能帮我看看我哪里错了吗?

这是我的 XML 文件数据:

<staff><info><staffid>test</staffid><email>testtest@gtes.com</email><surname>test</surname><givename>test</givename><address>test</address></info></staff>

这是我的 PHP/HTML 代码,我想在其中显示我的 html 记录。

<?php



                        $file="staff.xml";


                        $xml = simplexml_load_file($file) or die ("Error Loading Data");
                        foreach($xml->staff as $staff){

                        ?>
                        <tbody><tr>

                            <td><?php echo $staff->staffid; ?></td>
                            <td><?php echo $staff->email; ?></td>
                            <td><?php echo $staff->surname; ?></td>
                            <td><?php echo $staff->givename; ?></td>
                            <td><?php echo $staff->address; ?></td>

                        </tr>
                        </tbody>

                        <?php 
                            }
                        ?>

【问题讨论】:

    标签: php html xml


    【解决方案1】:

    您在解析该 xml 时有 incoreact 路径,更改您的 foreach 循环

    foreach($xml-&gt;staff as $staff)

    foreach ($xml-&gt;info as $stafs)

    我的工作测试代码

    $url = 'test.xml';
    
    $data = file_get_contents($url);
    
    $xml = simplexml_load_string($data);
    $enc = json_encode($xml);
    foreach ($xml->info as $staff) {
        echo $staff->staffid;
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2013-02-20
      • 1970-01-01
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      相关资源
      最近更新 更多