1

I have been trying to read the xml file, but it is giving me a strange error. My XML is as follows

<?xml version='1.0' encoding='UTF-8'?>
<response>
    <url>http://xyz.com</url>
    <token>xxxxxxx<token>
</response>

To read this I am using

simplexml_load_string(variable containing xml goes here)

but it is giving me this error

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

Warning: simplexml_load_string() [function.simplexml-load-string]: 1 in on line 47

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in on line 47

5
  • 3
    What encoding are you using to save your xml file? Or is the XML saved in a variable? Also, this <token>xxxxxxx<token> is not well-formed xml. Commented Oct 2, 2011 at 9:56
  • Same errors on codepad.org: codepad.org/0AsEZK8J There is a missing slash! Commented Oct 2, 2011 at 10:08
  • I am not saving the file here.I get this xml as a response from an API.My page encoding is <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> and doc type is <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> Commented Oct 2, 2011 at 11:35
  • I dont understand.where is the slash missing? Commented Oct 2, 2011 at 11:36
  • @hakre what about simplexml_load_string()?... knowing the source string is required to find out the actual problem, BOM or otherwise. Commented Oct 2, 2011 at 14:56

3 Answers 3

4

Thanx all for the attention and replies, But the problem lied somewhere else In my curl request the i had not set CURLOPT_RETURNTRANSFER to true and that was causing the XML not to be recorded in the variable and was somehow was getting printed on the screen giving the illusion that it is coming from the variable but it was not. However i set CURLOPT_RETURNTRANSFER to 1 and it is giving me correct results now. Sorry for the silly mistake. and thanx all.

Sign up to request clarification or add additional context in comments.

Comments

3

The response you get from the API is not well-formed/valid XML:

<token>xxxxxxx<token>
              ^ missing /

As this is an API response you need to fix it prior simplexml can actually read it in. You can make use of the tidy extension Docs to solve this:

$config = Array(
    'input-xml' => 1,
);
$xml = tidy_repair_string($xml, $config);

$xmlObj = simplexml_load_string($xml);

$doc = dom_import_simplexml($xmlObj)->ownerDocument;

$xpath = new DOMXpath($doc);

foreach($xpath->query('//token[not(node())]') as $node)
{
    $node->parentNode->removeChild($node);
}

echo $xmlObj->asXML();

This will produce the following XML by first fixing unclosed tags and then removing empty token elements:

<?xml version="1.0" encoding="utf-8"?>
<response>
<url>http://xyz.com</url>
<token>xxxxxxx
</token>
</response>

Related:

1 Comment

It's always the simple things. >.<;;;
0

As you rightly pointed out, CURLOPT_RETURNTRANSFER set to 1 is very important!

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 0 ); // set to 1 for debugging
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return from sms server

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.