【发布时间】: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->message()的问题。