<?xml version='1.0'?>
<error>
<error_detail code="0">Successfully</error_detail>
<error_detail code="1">Invalid Username or Password</error_detail>
<error_detail code="2">No username or password</error_detail>
<error_detail code="3">Session has expired</error_detail>
<error_detail code="4">Date of effectivity cannot be less than</error_detail>
</error>
如何使用xpath获取“会话已过期”?
我试过了:
$xml = simplexml_load_string($xml_string);
$a = $xml->xpath("//error_detail[@code='3']");
display_output($a);
我得到这个代替:
array ( 0 => SimpleXMLElement::__set_state(array( '@attributes' => array ( 'code' => '53', ), )), )
即使查询仅返回单个元素,xpath查询也将返回数组。您需要将第一个元素转换为字符串以获得文本值:
$xml = simplexml_load_string($xml_string);
$a = $xml->xpath("//error_detail[@code=3]");
// convert to string though . ''
var_dump($a[0] . ''); // string(19) "Session has expired"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句