0

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>
mugelloblue
  • 137
  • 1
  • 11

1 Answers1

2

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

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • @delboy1987uk Thanks so much for your answer. It works great when `$xml="entire soap request"` is in the body. However, I can't seem to get it to work when `$xml = "note.xml"` – mugelloblue Sep 20 '18 at 15:17
  • `note.xml` is a string that isn't XML. Did you mean `$xml = file_get_contents('note.xml');` ? – delboy1978uk Sep 20 '18 at 15:19
  • One more question. If I was looking to extract `marketClass` much like how you extracted `WorldManufacturerIdentifier` would I do something like this: `$id1 = $dom->getElementsByTagName('marketClass id="53"');` ? – mugelloblue Sep 20 '18 at 15:28
  • 1
    Xpath expressions allow you to use path, conditions and even casts: https://3v4l.org/8adtc – ThW Sep 20 '18 at 16:43
  • @ThW How would I use Xpath for something like modelYear which is embedded in `vinDescription` – mugelloblue Sep 20 '18 at 19:30
  • 1
    Use the attribute axis: `$xpath->evaluate('string(//v:vinDescription/@modelYear)');` – ThW Sep 21 '18 at 08:41
  • ThW thanks for expanding on this, I learned somethjing new! :-D – delboy1978uk Sep 21 '18 at 09:39