开发者

PHP: How to process SOAP response to get a tag value?

I've a SOAP response in a var $soap_response like this:

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0">
   <SOAP-ENV:Header>
      <h3:__MethodSignature xsi:type="SOAP-ENC:methodSignature" SOAP-ENC:root="1" xmlns:h3="http://schemas.microsoft.com/clr/soap/messageProperties" xmlns:a2="http://schemas.microsoft.com/clr/ns/System.Collections">xsd:string a2:Hashtable</h3:__MethodSignature>
   </SOAP-ENV:Header>
   <SOAP-ENV:Body>
      <i4:ReturnDataSetResponse id="ref-1" xmlns:i4="http://schemas.microsoft.com/clr/nsassem/TOIServerAppl.clsRSchedule/TOIServerAppl">
         <return href="#ref-6"/>
      </i4:ReturnDataSetResponse>
      <a3:DataSet id="ref-6" xmlns:a3="http://schemas.microsoft.com/clr/nsassem/System.Data/System.Data%2C%20Version%3D1.0.5000.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Db77a5c561934e089">
         <XmlSchema id="ref-7"><![CDATA[<?xml version="1.0" encoding="utf-16"?>
            <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">
                <xs:complexType>
                  <xs:choice maxOccurs="unbounded">
                    <xs:element name="Table">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element name="id" type="xs:long" msdata:targetNamespace="" minOccurs="0" />
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:choice>
                </xs:complexType>
              </xs:element>
            </xs:schema>]]>
        </XmlSchema>
        <XmlDiffGram id="ref-8">
            <id>4437031</id开发者_开发技巧>
        </XmlDiffGram>
      </a3:DataSet>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

How can I extract id value from <id>4437031</id>?

simplexml_load_string($soap_response);

returns empty object array.

I've seen someplaces that I might have to replace all those namespaces to make it work?


I was able to do it with SoapClient and __doRequest() function. Zend_Soap_client also uses SoapClient. So I chose SoapClient.


Let me save a whole lot of trouble and help you with your dataset instead (I'm assuming this is your web service, if it isn't I apologize).

Instead of serializing the whole dataset, run it through this function first and return it as a string.

Public Function FormatDataSet(ByVal ds As DataSet)
    Try
        Dim xmlstream As New StringBuilder
        Dim write As XmlWriter = XmlWriter.Create(xmlstream)
        write.WriteProcessingInstruction("xml", "version='1.0' encoding='utf-8'")
        ds.WriteXml(write)
        Return xmlstream.ToString()
    Catch ex As Exception
        Return ex.Message
    End Try
End Function

This will strip out .NET's schema and leave you with easily parsed XML (even SimpleXML will be able to parse it). You'll need System.Text and System.Xml

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜