Обрезать конверт SOAP для сохранения ответа в файл XML

0

У меня есть ответ 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="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"?>
           ...

Есть ли хороший способ снять этот конверт SOAP и просто сохранить что между ним?

Вот как я пишу ответ на файл:

$toWrite = htmlspecialchars_decode($client->__getLastResponse());
$fp = fopen('weather.xml', 'w');
fwrite($fp, $toWrite);
fclose($fp);
Теги:
soap
web-services

1 ответ

0

Проблема заключается в htmlspecialchars_decode(). Документ огибающей содержит другие XML-документы в виде текстовых узлов. Если вы декодируете объекты в документе XML, вы его уничтожите. Никогда не используйте htmlspecialchars_decode() в документе XML.

Загрузите (конверт) 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">
        &lt;?xml version="1.0"?>
        &lt;weather>XML&lt;/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>

Ещё вопросы

Сообщество Overcoder
Наверх
Меню