STR_用肥皂信封替换问题 [英] STR_Replace Issues With Soap Envelope

查看:28
本文介绍了STR_用肥皂信封替换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 str_replace() 函数从我的 Soap XML 输出中删除 S:EnvelopeS:Body 标签.尽管进行了多次尝试,但我始终无法删除这些标签.我需要删除标签,以便我可以使用诸如 echo $xml->vinDescription->WorldManufacturerIdentifier 之类的逻辑从 XML 输出中提取数据."<br>"; 使用 Soap 标签,我无法做到这一点.

I am trying to use the str_replace() function to remove the S:Envelope and S:Body tags from my Soap XML output. Despite many attempts I have been unable to remove these tags. I need to remove the tags so that I can pull data from the XML output using logic such as echo $xml->vinDescription->WorldManufacturerIdentifier . "<br>"; With the Soap tags present, I am unable to do this.

这是我的 XML 输出 (note.xml):

Here is my XML output (note.xml):

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
     <VehicleDescription xmlns="urn:description7b.services.chrome.com" country="US" language="en" modelYear="2008" bestMakeName="Audi" bestModelName="S4" bestStyleName="5dr Avant Wgn">
         <responseStatus responseCode="Successful" description="Successful"/>
      <vinDescription vin="WAUUL78E38A092113" modelYear="2008" division="Audi" modelName="S4" styleName="5dr Avant Wgn" bodyType="Wagon 4 Dr." drivingWheels="AWD" builddata="no">
         <WorldManufacturerIdentifier>Germany Audi Nsu</WorldManufacturerIdentifier>
      <restraintTypes>
         <group id="9">Safety</group>
         <header id="38">Air Bag - Frontal</header>
         <category id="1001">Driver Air Bag</category>
      </restraintTypes>
      <restraintTypes>
         <group id="9">Safety</group>
         <header id="38">Air Bag - Frontal</header>
         <category id="1002">Passenger Air Bag</category>
      </restraintTypes>
      <restraintTypes>
         <group id="9">Safety</group>
         <header id="39">Air Bag - Side</header>
         <category id="1005">Front Side Air Bag</category>
      </restraintTypes>
      <restraintTypes>
         <group id="9">Safety</group>
         <header id="39">Air Bag - Side</header>
         <category id="1007">Front Head Air Bag</category>
      </restraintTypes>
      <restraintTypes>
          <group id="9">Safety</group>
          <header id="39">Air Bag - Side</header>
          <category id="1008">Rear Head Air Bag</category>
       </restraintTypes>
       <marketClass id="53">Small Wagon</marketClass>
    </vinDescription>
  </VehicleDescription>
</S:Body>

还有我的带有 str_replace() 的 PHP 代码:

And my PHP code with the str_replace():

<html>
 <body>

    <?php
    $response = "note.xml";
    $clean_xml = str_replace(['<S:Body>','<S:Envelope  xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">'],'',  $response);

     $xml=simplexml_load_file($clean_xml) or die("Error: Cannot create object");
     echo $xml->vinDescription->WorldManufacturerIdentifier . "<br>";


   ?>

  </body>
 </html>

推荐答案

可以试试$xml->Envelope->Body->vinDescription->WorldManufacturerIdentifier.

应该尝试 DOMDocument!它比旧的简单 xml 好得多.举个例子:

You should try DOMDocument! It's way better than the old simple xml. Here's an example:

<?php

$dom = new DOMDocument();
$dom->loadXML($xml);

$node = $dom->getElementsByTagName('VehicleDescription')->item(0);
echo $dom->saveXml($node); // outputs xml without envelope and body

echo  "\n\n";

// But to answer your question..
$id = $dom->getElementsByTagName('WorldManufacturerIdentifier')->item(0);
echo $id->textContent;

您可以在此处查看:https://3v4l.org/7YKWO

查看 DOMDocument、DOMNode 和 DOMXPath 的手册:

Check the manual for DOMDocument, DOMNode, and DOMXPath:

http://php.net/manual/en/class.domdocument.php

http://php.net/manual/en/class.domnode.php

http://php.net/manual/en/class.domxpath.php

这篇关于STR_用肥皂信封替换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆