3

I'm having some trouble with an xml file, I have found all sorts of examples/guides around the web and on this site but I can't seem to see one like this:

I have an XML file like so:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<items item1="1" item2="2" item3="3" item4="4"/>
<items item1="1" item2="2" item3="3" item4="4"/>
<items item1="1" item2="2" item3="3" item4="4"/>
</root>

I've not seen the data set out like this before and when I try:

<?php

$obj = simplexml_load_string("sample.xml");
print_r($obj);
?>

I get the error:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found

I have run the xml file through the check at www.xmlvalidation.com and all comes back ok, am I missing something?

I am looking to be able to echo the value of the item attributes in a while loop.

2
  • is it really <root> at the end? not </root> ? Commented Jun 15, 2011 at 21:27
  • 1
    Hi, sorry this is a typo, the file I am using is correct. I will edit the post now, thanks for the quick reply – Commented Jun 15, 2011 at 21:29

3 Answers 3

8

You're attempting to load XML from a file with simplexml_load_string(), which expects a string input. Instead, use simplexml_load_file().

$obj = simplexml_load_file("sample.xml");
Sign up to request clarification or add additional context in comments.

1 Comment

Spot on, thanks! I'm just still a bit unsure why the file is set like this, do you have any ideas? Sorry I'm still very new.
0

One thing I noticed is that your closing tag on the last line should be </root> rather than <root>

3 Comments

Hi Andrew, sorry this is a typo, the file I am using is correct. I will edit the post now, thanks for the quick reply
I'm looking at it now to try to find the underlying cause. Changing the end tag doesn't fix it.
Found it, simplexml_load_string takes a string not a file. You want simplexml_load_file()... I'd edit my answer except someone else beat me to it so they deserve the credit :)
0

Your root tag at the end is not a closing tag. When I run this XML through the validator you mention, it fails.

Comments

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.