【问题标题】:Removing schema details from ouput xml and converting into array (Using PHP SoapClient and simplexml_load_string or SimpleXMLElement)从输出 xml 中删除模式详细信息并转换为数组(使用 PHP SoapClient 和 simplexml_load_string 或 SimpleXMLElement)
【发布时间】:2016-11-18 14:27:19
【问题描述】:

我收到来自第三方服务的 XML 响应,

$soap = new SoapClient($wsdl, $options);
$data = $soap->method($params);

响应看起来像 Stdclass 对象值

$data包含数组对象内的xml

$data->resultResponse->any包含以下xml格式,

<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet"><xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Temp" ..... </xs:element></xs:schema><diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><DocumentElement xmlns=""><Temp diffgr:id="Temp1" msdata:rowOrder="0"><name>Siva</name><age>18</age>....<location>chennai</chennai></Temp><Temp diffgr:id="Temp2" msdata:rowOrder="0"><name>John</name><age>18</age>....<location>chennai</chennai></Temp>

当这个 xml 用于 SimpleXMLElementsimplexml_load_string 进行数组转换时,我遇到以下错误,

$res = $data->resultResponse->any;
$res1 = new SimpleXMLElement($res);
$res2 = simplexml_load_string($res);

对于 $res1 和 $res2,

Warning: simplexml_load_string(): Entity: line 1: parser error : Extra content at the end of the document in soap.php on line 40

我不想要那个 xml 中的 "&lt;xs:schema"。我想要"&lt;diffgr:diffgram" 值。

以前,我为此使用了 nusoap 库。这对于较少的内存数据来说效果很好。 Nusoap 将整个 xml 转换为数组。但是,Php-Soap 客户端只提供第一级元素作为对象/键。

我已经在 DOM 和 preg_replace 中的 loadXml 中尝试了一些方法。但这些都没有多大帮助

请帮我解决问题。

从 Boomerang 获得的原始 xml 响应,

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ClaimMISResponse xmlns="http://tempuri.org/">
<ClaimMISResult>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Temp" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Temp">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string" minOccurs="0" />
...
<xs:element name="location" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<DocumentElement xmlns="">
<Temp diffgr:id="Temp1" msdata:rowOrder="0">
<name>John</name>
...
<location>Chennai</location>
</Temp>
</DocumentElement>
</diffgr:diffgram>
</ClaimMISResult>
</ClaimMISResponse>
</soap:Body>
</soap:Envelope>

【问题讨论】:

  • @RahulSingh 在我的问题中,问题是没有得到 simplexml_load_string 的响应。
  • 您发布的“XML”不完整且损坏。此外,它看起来像两个 XML 组合,一个 XML 文档只能有一个根元素节点。否则它是一个 XML 片段。 DOM 可以使用DOMDocumentFragment::appendXml() 加载 XML 片段。
  • @ThW 谢谢。该 xml 不是响应的原始内容。我现在已经更新了问题。请检查一下。
  • soap.php 上的代码是什么

标签: php xml soap xml-parsing soap-client


【解决方案1】:

很简单,用

分解你的结果
$result = \explode('</xs:schema>', $data->resultResponse->any);
$array = json_decode(json_encode((array)simplexml_load_string($result[1])),true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多