douxun7992 2012-12-18 09:08
浏览 102
已采纳

使用PHP解析带有命名空间的SOAP XML响应

I have this ugly XML which has alot of namespaces on it, when I try to load it with simpleXML if i indicate the first namespace I'd get an xml object ,but following tags with other namespaces would not make it to the object. Here is the SOAP response

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
            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">
   <SOAP-ENV:Header>
     <Interface PartnerID="FastBooking" PartnerType="Test" Version="1.0.0" />
     <Credentials HotelID="AC00006" User="User" Password="123456" />
     <Client Ref ="123456789" TimeStamp="2012-12-14T12:24:25+0100"/>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
     <ServiceNameRQ xmlns="urn:wsTest">
         <GetRatesRS>
             <Rates>
                 <Rate RateID="12984" FromDate="2010-05-12" ToDate="2010-06-30" RateType="PUBLIC" RateCode="RAC" MinRate="100" MaxRate="1000" Currency="EUR" ReadOnly="No" FromRateID="11456">
                    <RateName>Rack Rate</RateName>
                 </Rate>
                 <Rate RateID="13219" FromDate="2010-07-12" ToDate="2010-12-31" RateType="PUBLIC" RateCode="NORMAL" MinRate="100" MaxRate="1000" Currency="EUR" ReadOnly="Yes">
                    <RateName>Normal Rate</RateName>
                     <R1 Name="Single Occupancy" MinRate="90.00" MaxRate="1500.00" />
                     <R2 Name="Double Occupancy" MinRate="120.00" MaxRate="2000.00" />
                     <R3 Name="2 Nights" MinRate="150.00" MaxRate="2000.00" />
                    </Rate>
                     </Rates>
            </GetRatesRS>
        </ServiceNameRQ>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

Here is the PHP code I'm using:

$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);

But I can not retrieve the contents of SOAP-ENV:Body (GetRatesRS -> Rates -> Rate)

  • 写回答

1条回答 默认 最新

  • duanhuan7750 2012-12-18 09:17
    关注

    The rate data can be accessed like this:

    Demo

    $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 . "
    ";
    }
    

    Outputs

    Rack Rate
    Normal Rate

    If you want the Rate attributes, you can get them like this (within the loop):

    echo $rate->attributes()->RateID;
    

    You can read the R1 and R2 elements like this:

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建