STR_Replace Issues With Soap Envelope

懵懂的女人 提交于 2019-12-11 17:53:04

问题


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.

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>

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>

回答1:


You could try $xml->Envelope->Body->vinDescription->WorldManufacturerIdentifier.

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;

You can see it here: https://3v4l.org/7YKWO

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



来源:https://stackoverflow.com/questions/52427103/str-replace-issues-with-soap-envelope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!