解析xml命名空间web服务


Parsing xml namespace web service

我试图在QuoteDetail部分下解析命名空间Rate。下面是我从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;

尝试像下面这样包装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;
?>

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