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

hakre
  • 193,403
  • 52
  • 435
  • 836
techie_28
  • 2,123
  • 4
  • 41
  • 62
  • 3
    What encoding are you using to save your xml file? Or is the XML saved in a variable? Also, this `xxxxxxx` is not well-formed xml. – Shef Oct 02 '11 at 09:56
  • Same errors on codepad.org: http://codepad.org/0AsEZK8J There is a missing slash! – ComFreek Oct 02 '11 at 10:08
  • I am not saving the file here.I get this xml as a response from an API.My page encoding is and doc type is – techie_28 Oct 02 '11 at 11:35
  • I dont understand.where is the slash missing? – techie_28 Oct 02 '11 at 11:36
  • @hakre what about `simplexml_load_string()`?... knowing the source string is required to find out the actual problem, BOM or otherwise. – salathe Oct 02 '11 at 14:56

3 Answers3

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.

techie_28
  • 2,123
  • 4
  • 41
  • 62
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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
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