解析xml命名空间Web服务

时间:2015-08-10 15:18:38

标签: php xml web-services curl soap

我正在尝试解析Rate部分下的名称空间QuoteDetail。以下是我对Web服务的回复。获取Rate节点的任何帮助都很棒。

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<RateQuoteByAccountResponse xmlns="https://webservices.rrts.com/ratequote/">
<RateQuoteByAccountResult>
<QuoteNumber>2066833</QuoteNumber>
<NetCharge>676.75</NetCharge>
<Customer>
<AccountNumber>*******</AccountNumber>
<Name>*****</Name>
<Address1>**</Address1>
<Address2>**</Address2>
<City>***</City>
<State>**</State>
<ZipCode>*****</ZipCode>
</Customer>
<RoutingInfo>
<DestinationState>CA</DestinationState>
<DestinationZip>90210</DestinationZip>
<OriginState>NC</OriginState>
<OriginZip>27360</OriginZip>
<EstimatedTransitDays>5</EstimatedTransitDays>
<OriginTerminal>Charlotte</OriginTerminal>
</RoutingInfo>
<RateDetails>
<QuoteDetail>
<ActualClass>60</ActualClass>
<RatedClass>60</RatedClass>
<Charge>533.45</Charge>
<Code></Code>
<Description></Description>
<Rate>106.69</Rate>
<Weight>500</Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>41.95</Charge>
<Code>ID</Code>
<Description>Inside Delivery</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>32</Charge>
<Code>CFP</Code>
<Description>Prepaid COD Fee</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>69.35</Charge>
<Code>FSC</Code>
<Description>Fuel Surcharge - 13.00 %</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
</RateDetails>
<OriginType>O</OriginType>
<PaymentType>P</PaymentType>
<CODAmount>0</CODAmount>
<ShipmentDate>2105-08-07T00:00:00</ShipmentDate>
<CustomerCubicFoot>0</CustomerCubicFoot>
<HawaiianRatedCubicFoot>0</HawaiianRatedCubicFoot>
</RateQuoteByAccountResult>
</RateQuoteByAccountResponse>
</soap:Body>
</soap:Envelope>

这是我用来尝试解析但却收到错误的代码:Call to a member function children() on string

$xml = $curl->response;
$rate = (string)$xml->children('soap', true)->Body->RateQuoteByAccountResponse->RateQuoteByAccountResult->RateDetails->QuoteDetail->Rate;

1 个答案:

答案 0 :(得分:0)

尝试包装xml字符串,如下所示:

<?php
$string = '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<RateQuoteByAccountResponse xmlns="https://webservices.rrts.com/ratequote/">
<RateQuoteByAccountResult>
<QuoteNumber>2066833</QuoteNumber>
<NetCharge>676.75</NetCharge>
<Customer>
<AccountNumber>*******</AccountNumber>
<Name>*****</Name>
<Address1>**</Address1>
<Address2>**</Address2>
<City>***</City>
<State>**</State>
<ZipCode>*****</ZipCode>
</Customer>
<RoutingInfo>
<DestinationState>CA</DestinationState>
<DestinationZip>90210</DestinationZip>
<OriginState>NC</OriginState>
<OriginZip>27360</OriginZip>
<EstimatedTransitDays>5</EstimatedTransitDays>
<OriginTerminal>Charlotte</OriginTerminal>
</RoutingInfo>
<RateDetails>
<QuoteDetail>
<ActualClass>60</ActualClass>
<RatedClass>60</RatedClass>
<Charge>533.45</Charge>
<Code></Code>
<Description></Description>
<Rate>106.69</Rate>
<Weight>500</Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>41.95</Charge>
<Code>ID</Code>
<Description>Inside Delivery</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>32</Charge>
<Code>CFP</Code>
<Description>Prepaid COD Fee</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
<QuoteDetail>
<ActualClass></ActualClass>
<RatedClass></RatedClass>
<Charge>69.35</Charge>
<Code>FSC</Code>
<Description>Fuel Surcharge - 13.00 %</Description>
<Rate>0</Rate>
<Weight></Weight>
</QuoteDetail>
</RateDetails>
<OriginType>O</OriginType>
<PaymentType>P</PaymentType>
<CODAmount>0</CODAmount>
<ShipmentDate>2105-08-07T00:00:00</ShipmentDate>
<CustomerCubicFoot>0</CustomerCubicFoot>
<HawaiianRatedCubicFoot>0</HawaiianRatedCubicFoot>
</RateQuoteByAccountResult>
</RateQuoteByAccountResponse>
</soap:Body>
</soap:Envelope>';

$string = <<<XML
$string
XML;

$XmlArray = new SimpleXMLElement($string);
echo $ErrorCode = $XmlArray->children("soap", true)->Body->
                    children()->RateQuoteByAccountResponse->
                    children()->RateQuoteByAccountResult->children()->RateDetails->children()->QuoteDetail->children()->Rate;
?>

您将从列表中获得第一个评分--106.69。

相关问题