php xml解析 命名空间,使用PHP解析具有命名空间的SOAP XML响应

我有一个丑陋的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值