Home  >  Article  >  Backend Development  >  XML解析

XML解析

WBOY
WBOYOriginal
2016-06-23 14:19:51950browse

array(2) {
  ["header"]=>
  string(267) "HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=BDED717E8585DDA55A47EA1822C00E80; Path=/sdkproxy/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-GB
Content-Length: 103
Date: Sat, 24 Aug 2013 02:38:00 GMT
Connection: close"
  ["body"]=>
  string(103) "
0-1100.0"
}
是一个web服务端的返回值


用 simplexml_load_string解析返回false,我都诧异了

['body']的内容如何转换成一个数组形式


回复讨论(解决方案)

$xml_string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><response><error>0</error><message>-1100.0</message></response>";  $xml = simplexml_load_string($xml_string);  //对象转换成数组,方便引用  $arr = (array)$xml; var_dump($arr);

$ar = simplexml_load_string($data['body']);

$ar = simplexml_load_string($data['body']);

我还不至于犯这个低级错误

不知道你会犯什么样的高级错误

$s = '<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>';$xml = simplexml_load_string($s);print_r($xml);
SimpleXMLElement Object
(
    [error] => 0
    [message] => -1100.0
)

不知道你会犯什么样的高级错误

$s = '<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>';$xml = simplexml_load_string($s);print_r($xml);
SimpleXMLElement Object
(
    [error] => 0
    [message] => -1100.0
)

问题是我这里就是返回bool(false)

贴出接收到的数据的序列化值
比如你接受的的数据在 $data 中,则
echo serialize($data);
贴出结果

贴出接收到的数据的序列化值
比如你接受的的数据在 $data 中,则
echo serialize($data);
贴出结果


a:2:{s:6:"header";s:267:"HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=96B4D5E802AEBBE6A1F92824C576690B; Path=/sdkproxy/; HttpOnly
Content-Type: text/html;charset=UTF-8
Content-Language: en-GB
Content-Length: 103
Date: Sat, 24 Aug 2013 03:22:14 GMT
Connection: close";s:4:"body";s:103:"
0-1100.0";}

我感觉是数据的问题

$s = 'a:2:{s:6:"header";s:267:"HTTP/1.1 200 OKServer: Apache-Coyote/1.1Set-Cookie: JSESSIONID=96B4D5E802AEBBE6A1F92824C576690B; Path=/sdkproxy/; HttpOnlyContent-Type: text/html;charset=UTF-8Content-Language: en-GBContent-Length: 103Date: Sat, 24 Aug 2013 03:22:14 GMTConnection: close";s:4:"body";s:103:"<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>";}';$d = unserialize($s);$xml = simplexml_load_string(trim($d['body']));print_r($xml);
SimpleXMLElement Object
(
    [error] => 0
    [message] => -1100.0
)
你在获取数据时,没有切割干净。
http 数据头和数据体之间是“\r\n\r\n”,而你是按“\r\n”切割的
所以在数据体前面留有的“\r\n”造成分析失败

$s = 'a:2:{s:6:"header";s:267:"HTTP/1.1 200 OKServer: Apache-Coyote/1.1Set-Cookie: JSESSIONID=96B4D5E802AEBBE6A1F92824C576690B; Path=/sdkproxy/; HttpOnlyContent-Type: text/html;charset=UTF-8Content-Language: en-GBContent-Length: 103Date: Sat, 24 Aug 2013 03:22:14 GMTConnection: close";s:4:"body";s:103:"<?xml version="1.0" encoding="UTF-8"?><response><error>0</error><message>-1100.0</message></response>";}';$d = unserialize($s);$xml = simplexml_load_string(trim($d['body']));print_r($xml);
SimpleXMLElement Object
(
    [error] => 0
    [message] => -1100.0
)
你在获取数据时,没有切割干净。
http 数据头和数据体之间是“\r\n\r\n”,而你是按“\r\n”切割的
所以在数据体前面留有的“\r\n”造成分析失败

确实是这个原因,谢谢你的细心回答

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn