dongyao2022 2015-08-10 15:18
浏览 61
已采纳

解析xml命名空间Web服务[重复]

This question already has an answer here:

I am trying to parse the namespace Rate under the QuoteDetail section. Here is my response from the web service. Any help on getting that Rate node extracted would be awesome.

<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>

This is the code I am using to try and parse but getting error: Call to a member function children() on string

$xml = $curl->response;
$rate = (string)$xml->children('soap', true)->Body->RateQuoteByAccountResponse->RateQuoteByAccountResult->RateDetails->QuoteDetail->Rate;
</div>
  • 写回答

1条回答 默认 最新

  • dongwu8050 2015-08-10 15:28
    关注

    Try wrapping xml string around as follow:

    <?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;
    ?>
    

    You'll get first Rate - 106.69 from the list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的代码运行
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败