从输出xml中删除模式详细信息并转换为数组(使用PHP SoapClient和simplexml_load_string或SimpleXMLElement)

时间:2016-11-18 14:27:20

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

我从第三方服务获得XML响应,

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

响应看起来像Stdclass对象值

$ data 包含数组对象中的xml

$ data-> resultResponse->任何包含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使用 SimpleXMLElement simplexml_load_string 进行数组转换时,

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

对于$ res1&amp; $ 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的"<xs:schema"。我想要"<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>

1 个答案:

答案 0 :(得分:1)

太简单了,用

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