This question already has an answer here:
- How can I get useful error messages in PHP? 26 answers
I cannot display data from an XML file using the simple PHP parser. How do I fix it?
I cannot find the problem to this simple task and I am completely stuck. I am following a w3schools.com tutorial and the code won't display the data. I am using an apache web server on my raspberry pi.
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>
The page loads the txt file and displays it fine.
</div>