3

i have a xml file like this

<?xml version="1.0"?>
<test>Total</test>
<books>
    <mybook id="1">
        <blabla>jkjk</blabla>
    </mybook>¨
</books>

If I parse it like that with simpleXML

$xml = simplexml_load_file($filename); 

it says

simplexml_load_file() [function.simplexml-load-file]: ./uploads/test.xml:3: parser error : Extra content at the end of the document

But if a remove the

<test>Total</test>

at the begining of the xml file it works....

any ideas ?? thank you

3 Answers 3

13

You need to have a root element in your xml, i.e.

<document>
    <test>Total</test>
    <books>
        <mybook id="1">
            <blabla>jkjk</blabla>
        </mybook>
    </books>
</document>
Sign up to request clarification or add additional context in comments.

Comments

2

Your XML is invalid. XML must have one and only one root element.

The "extra content" the parser is complaining about is the <books> element.

Comments

2

It is what it writes. Extra content.

And what is it? ¨ after </mybook>. So the file isn't valid. If you want it valid, it'd look like:

    <bookinfo>
   <test>Total</test>
    <books>
        <mybook id="1">
            <blabla>jkjk</blabla>
        </mybook>
    </books>
    </bookinfo>

A note: an XML has to be always ONE, exactly one root element.

2 Comments

Whoever minussed this: Why? This is totally correct answer. Nearly same as Petter's (+4), but one minutes before. Any reason? I want to know.
@axiomer I downvoted this prior to your correction, but didn't return before I could remove it and saw the correction. A shallow edit will fix this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.