0

I am trying to parse an xml document using php simplexml_load_file() function.

I have a demo.xml file in the site root

demo.xml

<?xml version="1.0"encoding="UTF-8" ?>
<sms>
<sender>foo bar</sender>
 <sentto>john doe</sentto>
 </sms>

And I am trying to get the value of sender , the following is my php code :

<?php

$xml=simplexml_load_file("demo.xml");
echo $xml->sender;

But I am getting the parser error whenever i try to load the php file.

Parser error : blank needed here in www/htdocs/read.php on line 3.

How can I solve it?

Thanks ,SK.

1 Answer 1

3

The problem is here

<?xml version="1.0"encoding="UTF-8" ?>
------------------^

A space is needed, change it to

<?xml version="1.0" encoding="UTF-8" ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, with your xml ,now I am getting this error : Xml declaration allowed only at the start of the document on line 3 ,Do you know what it means and how to get rid of it?
maybe the contents of your demo.xml start with some newline?

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.