STR_Replace 肥皂信封问题

STR_Replace Issues With Soap Envelope

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

这是我的 XML 输出 (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>

我的 PHP 代码 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 好多了。这是一个例子:

<?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 的手册:

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

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

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