user1092780
user1092780

Reputation: 2457

Is this correct XML formatting for PHP parsing?

Is the following format correct for XML? I am trying to parse the file in PHP using simplexml_load_file but keep receiving an error stating that there is 'Extra content at the end of the document on line 3'.

Here is the XML file:

<?xml version="1.0"?>
  <title>Test File</title>
  <data>
    <record id="1">
      <department>
        <name>ACME</name>
        <number>5</number>
      </dep_name>
      <floor>
        <name>ACME Floor</name>
        <number>5</number>
      </floor>
    </record>
  </data>

Upvotes: 0

Views: 151

Answers (2)

afuzzyllama
afuzzyllama

Reputation: 6548

you need to have a root for your structure:

<?xml version="1.0"?>
<xml>
    <title>Test File</title>
    <data>
        <record id="1">
            <department>
                <name>ACME</name>
                <number>5</number>
            </department>
            <floor>
                <name>ACME Floor</name>
                <number>5</number>
            </floor>
        </record>
    </data>
</xml>

or else your XML parser will think that your root is <title> and that with in it all your information is contained.

Also, <department> needs to be closed by </department>, which has been reflected in my answer.

Upvotes: 2

Quentin
Quentin

Reputation: 944293

An XML document must consist of a single root element and its descendants.

Your first start tag is <title>. It has a text node as its sole descendent. You then have a </title> tag which ends the root element and thus the document.

<data> and everything following it is therefore an error.

You probably want to create a new element type to be the root (and wrap the entire document, excluding the XML declaration, with it).

You also need to correct either <department> or </dep_name> as you appear to have changed the name of the element halfway through creating it.

You should run your code through a validator or a lint, as that will highlight errors.

Upvotes: 6

Related Questions