0

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.

Harry Saliba
  • 116
  • 1
  • 11
  • You may want to enable PHP's error displaying (if you're not using your own machine, it's probably best to `ini_set("display_errors", 1);`). You'd have caught the `$XMLFile->message()` problem. – user4520 Mar 05 '15 at 22:20

2 Answers2

3

As just said XML must have 1 root element. Moreover, when you use the foreach construct, you are trying to call the method message(), while it's a property:

foreach($XMLFile->message as $data) {
    echo $data->subject;
    echo $data->recipient;
    echo $data->sender;
}
Federico Ginosa
  • 316
  • 2
  • 6
2

XML can only have 1 root element. See http://en.wikipedia.org/wiki/Root_element

You need to change your XML to:

<?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>
AlexL
  • 1,699
  • 12
  • 20