从xml文件php获取属性
您需要导航到具有所需属性的元素。有很多方法从xml文件php获取属性,php,xml,xml-parsing,attributes,simplexml,Php,Xml,Xml Parsing,Attributes,Simplexml,您需要导航到具有所需属性的元素。有很多方法 $results = simplexml_load_string($response); $errorCode = $results->attributes()->{'errors'}; 由于只有一条错误消息和一条错误,因此这种方法非常有效。如果有多个,可以使用数组表示法来表示所需的数组。因此,下面的行也回显302 echo $results->{'error-messages'}->errors['code'];//3
$results = simplexml_load_string($response);
$errorCode = $results->attributes()->{'errors'};
由于只有一条错误消息
和一条错误
,因此这种方法非常有效。如果有多个,可以使用数组表示法来表示所需的数组。因此,下面的行也回显302
echo $results->{'error-messages'}->errors['code'];//302
您甚至可以使用xpath
,一种查询语言来遍历xml。/
将按名称返回所有节点:
echo $results->{'error-messages'}[0]->errors[0]['code'];
echo
显示一个数字,但它仍然是一个对象。如果只想捕获整数,请按如下方式进行转换:
echo $results->xpath('//errors')[0]->attributes()->code; //302
看看这个
echo $results->xpath('//errors')[0]->attributes()->code; //302
$errorCode = (int) $results->{'error-messages'}->errors['code'];