我有一个丑陋的XML,上面有很多名称空间,当我尝试使用simpleXML加载它(如果我指示第一个名称空间)时,我会得到一个xml对象,但是后面带有其他名称空间的标记不会将其添加到该对象.
这是SOAP响应
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns8502="http://tempuri.org">
Rack Rate
Normal Rate
这是我正在使用的PHP代码:
$result = $xml_query;
$result = stripslashes($result);
$result = simplexml_load_string($result);
$namespacesMeta = $result->getNamespaces(true);
$mediaXML = $result->children($namespacesMeta['SOAP-ENV:Header']);
$Interface = $mediaXML->xpath('//Interface');
$Credentials = $mediaXML->xpath('//Credentials');
$Client = $mediaXML->xpath('//Client');
$attributesInterface = $Interface[0]['@attributes'];
$PartnerID = $attributesInterface['PartnerID'];
$PartnerType = $attributesInterface['PartnerType'];
$Version = $attributesInterface['Version'];
$attributesCredentials = $Credentials[0]['@attributes'];
$HotelID = $attributesCredentials['HotelID'];
$User = $attributesCredentials['User'];
$Password = $attributesCredentials['Password'];
$HotelID = filter_var($HotelID, FILTER_SANITIZE_MAGIC_QUOTES);
$User = filter_var($User, FILTER_SANITIZE_MAGIC_QUOTES);
$Password = filter_var($Password, FILTER_SANITIZE_MAGIC_QUOTES);
$Password = tchag3iba($Password);
但是我无法检索SOAP-ENV:Body的内容(GetRatesRS-> Rates-> Rate)
解决方法:
可以通过以下方式访问费率数据:
$obj = simplexml_load_string($xml);
foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
{
echo (string)$rate->RateName . "\n";
}
输出
Rack Rate
Normal Rate
如果需要Rate属性,则可以这样获得它们(在循环内):
echo $rate->attributes()->RateID;
您可以像这样读取R1和R2元素:
foreach($obj->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('urn:wsTest')->ServiceNameRQ->GetRatesRS->Rates->Rate as $rate)
{
if(isset($rate->R1))
{
echo $rate->R1->attributes()->Name;
echo $rate->R1->attributes()->MinRate;
}
if(isset($rate->R2))
{
echo $rate->R2->attributes()->Name;
echo $rate->R2->attributes()->MinRate;
}
}
标签:php,xml,soap,namespaces,simplexml
来源: https://codeday.me/bug/20191011/1895073.html