繁体   English   中英

PHP解析SOAP XML响应

[英]PHP Parse SOAP XML Response

尽管我尝试使用namespaces和simpleXML,但我仍然不知道如何解析该XML响应,但仍然没有结果……任何想法?

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <ns3:GetDistrictByAddressResponse xmlns:ns3="http://il/co/bar/webservices/getdistrictbyaddress">
            <TimeFrameTable>
                <CustomerNumber>250</CustomerNumber>
                <Row>
                    <WindowDate>10052016</WindowDate>
                    <WeekDay>Sunday</WeekDay>
                    <FromHour>1130</FromHour>
                    <ToHour>1430</ToHour>
                </Row>
                <Row>
                    <WindowDate>10052016</WindowDate>
                    <WeekDay>Sunday</WeekDay>
                    <FromHour>1430</FromHour>
                    <ToHour>1730</ToHour>
                </Row>
            </TimeFrameTable>
        </ns3:GetDistrictByAddressResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

xpath是您的朋友:

xpath('//Row');

完整示例:

$soap = <<< LOL
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header />
    <SOAP-ENV:Body>
        <ns3:GetDistrictByAddressResponse xmlns:ns3="http://il/co/bar/webservices/getdistrictbyaddress">
            <TimeFrameTable>
                <CustomerNumber>250</CustomerNumber>
                <Row>
                    <WindowDate>10052016</WindowDate>
                    <WeekDay>Sunday</WeekDay>
                    <FromHour>1130</FromHour>
                    <ToHour>1430</ToHour>
                </Row>
                <Row>
                    <WindowDate>10052016</WindowDate>
                    <WeekDay>Sunday</WeekDay>
                    <FromHour>1430</FromHour>
                    <ToHour>1730</ToHour>
                </Row>
            </TimeFrameTable>
        </ns3:GetDistrictByAddressResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
LOL;
$xml = simplexml_load_string($soap);
foreach ($xml->xpath('//Row') as $item)
{
    print_r($item);
}

输出:

SimpleXMLElement Object
(
    [WindowDate] => 10052016
    [WeekDay] => Sunday
    [FromHour] => 1130
    [ToHour] => 1430
)
SimpleXMLElement Object
(
    [WindowDate] => 10052016
    [WeekDay] => Sunday
    [FromHour] => 1430
    [ToHour] => 1730
)

Ideone演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM