1
votes

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

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. - Shef
Same errors on codepad.org: codepad.org/0AsEZK8J There is a missing slash! - ComFreek
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"> - techie_28
I dont understand.where is the slash missing? - techie_28
@hakre what about simplexml_load_string()?... knowing the source string is required to find out the actual problem, BOM or otherwise. - salathe

3 Answers

4
votes

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.

3
votes

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:

0
votes

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