从输出xml中删除模式细节并转换为数组(使用PHP SoapClient和simplexml_load_string或S


Removing schema details from ouput xml and converting into array (Using PHP SoapClient and simplexml_load_string or SimpleXMLElement)

我从第三方服务获得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使用在SimpleXMLElementsimplexml_load_string数组转换,

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

$res1 &$它

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"

在此之前,我使用了nussoap库。对于更少的内存数据,这是很好的工作。nussoap将整个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>

这很简单,使用

$result = 'explode('</xs:schema>', $data->resultResponse->any);
$array = json_decode(json_encode((array)simplexml_load_string($result[1])),true);