Php 剥离SOAP信封以将响应保存到XML文件
我有一个SOAP响应,希望将其保存到XML文件中。将响应写入文件时,SOAP信封也随之写入,这使得XML文件由于以下错误而变得无用:Php 剥离SOAP信封以将响应保存到XML文件,php,xml,web-services,soap,Php,Xml,Web Services,Soap,我有一个SOAP响应,希望将其保存到XML文件中。将响应写入文件时,SOAP信封也随之写入,这使得XML文件由于以下错误而变得无用: XML declaration allowed only at the start of the document in ... 在本例中,XML被声明两次: <?xml version="1.0" encoding="ISO-8859-1"?> <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="ht
XML declaration allowed only at the start of the document in ...
在本例中,XML被声明两次:
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><ns1:NDFDgenResponse xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<dwmlOut xsi:type="xsd:string">
<?xml version="1.0"?>
...
问题在于htmlspecialchars_解码。信封文档包含其他XML文档作为文本节点。如果对XML文档中的实体进行解码,就会破坏它。切勿在XML文档上使用htmlspecialchars\u解码
将信封XML加载到DOM中并从中读取所需的值
$xml = <<<'XML'
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:NDFDgenResponse
xmlns:ns1="http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl">
<dwmlOut xsi:type="xsd:string">
<?xml version="1.0"?>
<weather>XML</weather>
</dwmlOut>
</ns1:NDFDgenResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xpath->registerNamespace('ndfd', 'http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl');
$innerXml = $xpath->evaluate(
'string(/soap:Envelope/soap:Body/ndfd:NDFDgenResponse/dwmlOut)'
);
echo $innerXml;
输出:
<?xml version="1.0"?>
<weather>XML</weather>
<?xml version="1.0"?>
<weather>XML</weather>