我正在尝试使用str_replace()
函数从Soap XML输出中删除S:Envelope
和S: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>
答案 0 :(得分:2)
您可以尝试$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;
您可以在这里看到它:documentation
查看DOMDocument,DOMNode和DOMXPath的手册: