soap解析与simplexml_load_string我想解析它我得到空身体的响应

时间:2010-07-29 07:47:10

标签: soap simplexml

<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <SearchHotelsResponse xmlns="http://tourico.com/webservices/">
            <SearchHotelsResult>
                <xs:schema id="TWS_HotelList" targetNamespace="http://tourico.com/webservices/TWS_HotelList.xsd" xmlns:mstns="http://tourico.com/webservices/TWS_HotelList.xsd" xmlns="http://tourico.com/webservices/TWS_HotelList.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
                xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
                    <xs:element name="TWS_HotelList" msdata:IsDataSet="true" msdata:Locale="en-US" msdata:EnforceConstraints="False">
                        <xs:complexType>
                            <xs:choice minOccurs="0" maxOccurs="unbounded">
                                <xs:element name="Hotel">
                                    <xs:complexType>
                                        <xs:sequence>
                                            <xs:element name="RoomType" minOccurs="0" maxOccurs="unbounded">
                                                <xs:complexType>
                                                    <xs:sequence>
                                                        <xs:element name="Avail" minOccurs="0" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                <xs:attribute name="offset" form="unqualified" type="xs:byte" use="required" />
                                                                <xs:attribute name="status" form="unqualified" type="xs:boolean" use="required" /></xs:complexType>
                                                        </xs:element>
                                                        <xs:element name="Discount" minOccurs="0" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                <xs:attribute name="type" form="unqualified" msprop:Generator_UserColumnName="type" msprop:Generator_ColumnVarNameInTable="columtype" type="xs:string" use="required" />
                                                                <xs:attribute name="name" form="unqualified" type="xs:string" use="required" />
                                                                <xs:attribute name="from" form="unqualified" msdata:DateTimeMode="Unspecified" type="xs:dateTime" use="required" />
                                                                <xs:attribute name="to" form="unqualified" msdata:DateTimeMode="Unspecified" type="xs:dateTime" use="required" />
                                                                <xs:attribute name="value" form="unqualified" type="xs:string" use="required" /></xs:complexType>
                                                        </xs:element>
                                                        <xs:element name="Occup" minOccurs="0" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                <xs:sequence>
                                                                    <xs:element name="Supp" minOccurs="0" maxOccurs="unbounded">
                                                                        <xs:complexType>
                                                                            <xs:attribute name="supId" form="unqualified" type="xs:int" />
                                                                            <xs:attribute name="name" form="unqualified" type="xs:string" use="required" />
                                                                            <xs:attribute name="price" form="unqualified" type="xs:double" use="required" /></xs:complexType>
                                                                    </xs:element>
                                                                    <xs:element name="Board" minOccurs="0" maxOccurs="unbounded">
                                                                        <xs:complexType>
                                                                            <xs:attribute name="bbId" form="unqualified" type="xs:int" />
                                                                            <xs:attribute name="name" form="unqualified" type="xs:string" use="required" />
                                                                            <xs:attribute name="price" form="unqualified" type="xs:double" use="required" />
                                                                            <xs:attribute name="default" form="unqualified" msprop:Generator_UserColumnName="default" msprop:Generator_ColumnVarNameInTable="columndefault" msprop:Generator_ColumnPropNameInTable="defaultColumn"
                                                                            type="xs:boolean" default="false" /></xs:complexType>
                                                                    </xs:element>
                                                                    <xs:element name="Room" minOccurs="0" maxOccurs="unbounded">
                                                                        <xs:complexType>
                                                                            <xs:sequence>
                                                                                <xs:element name="Child" minOccurs="0" maxOccurs="unbounded">
                                                                                    <xs:complexType>
                                                                                        <xs:attribute name="age" form="unqualified" type="xs:int" /></xs:complexType>
                                                                                </xs:element>
                                                                            </xs:sequence>
                                                                            <xs:attribute name="seqNum" form="unqualified" type="xs:int" use="required" />
                                                                            <xs:attribute name="adultNum" form="unqualified" type="xs:int" use="required" />
                                                                            <xs:attribute name="childNum" form="unqualified" type="xs:int" /></xs:complexType>
                                                                    </xs:element>
                                                                    <xs:element name="Price" minOccurs="0" maxOccurs="unbounded">
                                                                        <xs:complexType>
                                                                            <xs:attribute name="offset" form="unqualified" type="xs:int" use="required" />
                                                                            <xs:attribute name="value" form="unqualified" type="xs:double" use="required" /></xs:complexType>
                                                                    </xs:element>
                                                                </xs:sequence>
                                                                <xs:attribute name="occupId" form="unqualified" type="xs:string" use="required" />
                                                                <xs:attribute name="maxAdult" form="unqualified" type="xs:int" use="required" />
                                                                <xs:attribute name="maxChild" form="unqualified" type="xs:int" use="required" />
                                                                <xs:attribute name="price" form="unqualified" type="xs:double" use="required" />
                                                                <xs:attribute name="tax" form="unqualified" type="xs:double" use="required" />
                                                                <xs:attribute name="dblBed" form="unqualified" type="xs:boolean" />
                                                                <xs:attribute name="avrNightPrice" form="unqualified" type="xs:double" use="required" /></xs:complexType>
                                                        </xs:element>
                                                    </xs:sequence>
                                                    <xs:attribute name="productId" form="unqualified" type="xs:string" use="required" />
                                                    <xs:attribute name="name" form="unqualified" type="xs:string" use="required" />
                                                    <xs:attribute name="nights" form="unqualified" type="xs:byte" use="required" />
                                                    <xs:attribute name="startDate" form="unqualified" type="xs:dateTime" use="required" />
                                                    <xs:attribute name="isAvailable" form="unqualified" type="xs:boolean" />
                                                    <xs:attribute name="roomId" form="unqualified" type="xs:int" />
                                                    <xs:attribute name="hotelRoomTypeId" form="unqualified" type="xs:int" /></xs:complexType>
                                            </xs:element>
                                        </xs:sequence>
                                        <xs:attribute name="hotelId" form="unqualified" type="xs:int" use="required" />
                                        <xs:attribute name="provider" form="unqualified" type="xs:string" use="required" />
                                        <xs:attribute name="name" form="unqualified" type="xs:string" use="required" />
                                        <xs:attribute name="address" form="unqualified" type="xs:string" />
                                        <xs:attribute name="category" form="unqualified" type="xs:string" />
                                        <xs:attribute name="bestVal" form="unqualified" type="xs:byte" />
                                        <xs:attribute name="thumb" form="unqualified" type="xs:string" />
                                        <xs:attribute name="starsLevel" form="unqualified" type="xs:double" />
                                        <xs:attribute name="minAverPrice" form="unqualified" type="xs:double" use="required" />
                                        <xs:attribute name="desc" form="unqualified" type="xs:string" use="required" />
                                        <xs:attribute name="location" form="unqualified" type="xs:string" use="required" />
                                        <xs:attribute name="currency" form="unqualified" type="xs:string" />
                                        <xs:attribute name="brandId" form="unqualified" type="xs:int" />
                                        <xs:attribute name="brandName" form="unqualified" type="xs:string" /></xs:complexType>
                                </xs:element>
                            </xs:choice>
                        </xs:complexType>
                    </xs:element>
                </xs:schema>
                <TWS_HotelList xmlns="http://tourico.com/webservices/TWS_HotelList.xsd">
                    <Hotel hotelId="1041291" provider="localTgsProvider" name="Rush Inn - DEMO - DEMO" address="DEMO - Khalid Bin Waleed Road Dubai 44099" category="Tourist Class" bestVal="0" thumb="http://image1.urlforimages.com/1041291/Rush-Front_Thumbnailed.jpg"
                    starsLevel="2" minAverPrice="148.54" desc="DEMO - DEMO - Families will love to vacation at the Rush Inn with all the amenities and attractions it provides" location="Bur Dubai,Dubai,AE" currency="AED" brandId="0" brandName="">
                        <RoomType productId="1041291;1180012;34538" name="Standard" nights="1" startDate="2010-08-20T00:00:00-04:00" isAvailable="true" roomId="3" hotelRoomTypeId="1180012">
                            <Avail offset="0" status="true" />
                            <Occup occupId="1041291;1180012;34538;2;2;0;1" maxAdult="4" maxChild="2" price="148.54" tax="0" dblBed="true" avrNightPrice="148.54">
                                <Board bbId="24" name="Room Only" price="0" default="true" />
                                <Room seqNum="1" adultNum="1" childNum="1">
                                    <Child age="8" />
                                </Room>
                                <Price offset="0" value="148.54" />
                            </Occup>
                            <Occup occupId="1041291;1180012;34538;2;2;0;2" maxAdult="4" maxChild="2" price="148.54" tax="0" dblBed="false" avrNightPrice="148.54">
                                <Board bbId="24" name="Room Only" price="0" default="true" />
                                <Room seqNum="1" adultNum="1" childNum="1">
                                    <Child age="8" />
                                </Room>
                                <Price offset="0" value="148.54" />
                            </Occup>
                        </RoomType>
                    </Hotel>
                </TWS_HotelList>
            </SearchHotelsResult>
        </SearchHotelsResponse>
    </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:2)

我敢打赌,问题是当你有一个包含在XML数据包中的SOAP数据时,PHP中的函数可以用来提取SOAP数据吗?

我刚研究了一个小时,因为我遇到了类似的问题。可归结为仅仅因为SOAP包含在XML中并不意味着您可以使用“简单”;-) XML函数来提取所需的SOAP信息。

您必须牢记命名空间。就我而言,我有这个:

<?xml version="1.0" encoding="utf-8"?>
 * <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 * <soap:Body><Request_MerchantStatementResponse xmlns="http://www.netcash.co.za/netserv/ncUpload">
 * <Request_MerchantStatementResult>2010/11/06 00:00:00\tOBL\t0\t\tOpening Balance\t100000.00\t+\t0.00\t\t\t\t\t\t\t\r</Request_MerchantStatementResult>
 * </Request_MerchantStatementResponse></soap:Body>
 * </soap:Envelope>

我需要提取“Request_MerchantStatementResult”并且只提取它。解决方案是指定返回XML中引用的名称空间。

    $xml = simplexml_load_string($response);

    echo $xml->children('http://schemas.xmlsoap.org/soap/envelope/')
->children('http://www.netcash.co.za/netserv/ncUpload')
->Request_MerchantStatementResponse
->Request_MerchantStatementResult;

如果你只是print_r来自simplexml_load_string的结果,那么问题就出现了。所以一定要指定你的名字空间。

修改

我离题了,而不是

echo $xml->children('http://schemas.xmlsoap.org/soap/envelope/')
    ->children('http://www.netcash.co.za/netserv/ncUpload')
    ->Request_MerchantStatementResponse
    ->Request_MerchantStatementResult;

我应该有:

$result = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')
    ->children('http://www.netcash.co.za/netserv/ncUpload')
    ->Request_MerchantStatementResponse;

echo $result->Request_MerchantStatementResult;