开发者

Can't read an XML file with simplexml_load_string

开发者 https://www.devze.com 2023-04-10 05:51 出处:网络
I have been trying to read the xml file, but it is giving m开发者_Python百科e a strange error. My XML is as follows

I have been trying to read the xml file, but it is giving m开发者_Python百科e 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


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.


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:

  • PHP SimpleXML - Remove xpath node


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
0

精彩评论

暂无评论...
验证码 换一张
取 消