【问题标题】:PHP: How to process SOAP response to get a tag value?PHP:如何处理 SOAP 响应以获取标记值?
【发布时间】:2010-05-28 09:22:27
【问题描述】:

我在 var $soap_response 中有一个 SOAP 响应,如下所示:

<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>

如何从 &lt;id&gt;4437031&lt;/id&gt; 中提取 id 值?

simplexml_load_string($soap_response);

返回空对象数组。

我见过一些地方我可能必须替换所有这些命名空间才能使其正常工作?

【问题讨论】:

标签: php xml soap


【解决方案1】:

我可以使用 SoapClient 和 __doRequest() 函数来做到这一点。 Zend_Soap_client 也使用 SoapClient。所以我选择了 SoapClient。

【讨论】:

    【解决方案2】:

    让我省去一大堆麻烦,并帮助您处理您的数据集(我假设这是您的网络服务,如果不是,我深表歉意)。

    不要序列化整个数据集,而是先通过这个函数运行它并将其作为字符串返回。

    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
    

    这将去除 .NET 的架构,并为您提供易于解析的 XML(甚至 SimpleXML 也能够解析它)。你需要 System.Text 和 System.Xml

    【讨论】:

    • 感谢您的努力,但这不是我的网络服务。
    • 太糟糕了 - 构建 .NET Web 服务的人似乎总是决定返回复杂的对象。
    • 对我来说,我看到太多使用非常开放模式的 .NET 平台上生成的 Web 服务,并且太多返回 XML“blob”作为字符串并希望您事后解析它。
    猜你喜欢
    • 2021-12-13
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多