【问题标题】:PHP simple XML parser not returning data [duplicate]PHP简单XML解析器不返回数据[重复]
【发布时间】:2015-05-07 08:09:55
【问题描述】:

我无法使用简单的 PHP 解析器显示 XML 文件中的数据。我该如何解决?

我找不到这个简单任务的问题,我完全陷入困境。我正在关注 w3schools.com 教程,代码不会显示数据。我在我的树莓派上使用 apache 网络服务器。

PHP:

<html>
    <head>
        <style>
            code {
                font-family: ;
            } div {
                margin-top: 5px;
                margin-bottom: 5px;
                margin-left: 5px;
                margin-right: 5px;
                background-color: rgb(177,177,177);
                height: 200px;
                width: 300px;
            }
        </style>
    </head>
    <body>
        <center><h1>Hello BISD! This is a perfectly fine webpage!</h1></center>
        <p>TXT File:</p>
        <div>
        <?php
        $file=fopen("placeholder.txt","r");
        while(!feof($file)) {
            echo fgets($file).'<br>';
        }
        fclose($file);
        ?>
        </div>
        <p>XML File:</p>
        <div>
        <?php
        $XMLFile = simplexml_load_file("test.xml");
        if ($XMLFile === false) {
            echo "Failed loading XML: ";
            foreach(libxml_get_errors() as $error) {
                echo "<br>", $error->message;
            }
        } else {
            foreach($XMLFile->message() as $data) {
                echo $data->subject;
                echo $data->recipient;
                echo $data->sender;
            }
        }
        ?>
        </div>
    </body>
</html>

XML:

<?xml version='1.0' encoding='UTF-8'?>
<message>
    <subject>Test XML File</subject>
    <sender>Harry</sender>
    <recipient>The Internet</recipient>
    <content>Hello world!</content>
</message>
<message>
    <subject>Regarding XML</subject>
    <sender>Harry</sender>
    <recipient>The Internet</recipient>
    <content>XML is awesome!</content>
</message>

页面加载txt文件并正常显示。

【问题讨论】:

  • 您可能想要启用 PHP 的错误显示(如果您不使用自己的机器,最好ini_set("display_errors", 1);)。你会发现$XMLFile-&gt;message() 的问题。

标签: php html xml


【解决方案1】:

正如刚才所说,XML 必须有 1 个根元素。此外,当您使用 foreach 构造时,您会尝试调用方法 message(),而它是一个属性:

foreach($XMLFile->message as $data) {
    echo $data->subject;
    echo $data->recipient;
    echo $data->sender;
}

【讨论】:

    【解决方案2】:

    XML 只能有 1 个根元素。见http://en.wikipedia.org/wiki/Root_element

    您需要将您的 XML 更改为:

    <?xml version='1.0' encoding='UTF-8'?>
    <messages>
        <message>
            <subject>Test XML File</subject>
            <sender>Harry</sender>
            <recipient>The Internet</recipient>
            <content>Hello world!</content>
        </message>
        <message>
            <subject>Regarding XML</subject>
            <sender>Harry</sender>
            <recipient>The Internet</recipient>
            <content>XML is awesome!</content>
        </message>
    </messages>
    

    【讨论】:

    • 我稍后会尝试这个,我现在很忙,但如果它有效,我会提名你的答案。
    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    相关资源
    最近更新 更多