所以我想解析这个XML:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<requestContactResponse xmlns="http://webservice.foo.com">
<requestContactReturn>
<errorCode xsi:nil="true"/>
<errorDesc xsi:nil="true"/>
<id>744</id>
</requestContactReturn>
</requestContactResponse>
</soapenv:Body>
</soapenv:Envelope>
具体来说,我想要得到的变量的值<id>
这是我的尝试:
$dom = new DOMDocument;
$dom->loadXML($xml);
$dom->children('soapenv', true)->Envelope->children('soapenv', true)->Body->children()->requestContactResponse->requestContactReturn->id;
但我收到此错误信息:
PHP致命错误:调用未定义的方法的DOMDocument ::儿童()
我也尝试使用SimpleXML的:
$sxe = new SimpleXMLElement($xml);
$sxe->children('soapenv', true)->Envelope->children('soapenv', true)->Body->children()->requestContactResponse->requestContactReturn->id;
但是,我得到这个其他错误信息:
PHP致命错误:调用一个成员函数的孩子()一个非对象
最后的解决方案我已经试过:
$sxe = new SimpleXMLElement($xml);
$elements = $sxe->children("soapenv", true)->Body->requestContactResponse->requestContactReturn;
foreach($elements as $element) {
echo "|-$element->id-|";
}
这一次的错误信息是:
Invalid argument supplied for foreach()
有什么建议?