用GSOAP解析肥皂味精

Parse soap msg with gSOAP

本文关键字:肥皂 GSOAP      更新时间:2023-10-16

我有以下示例xml消息:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header>
        <to>...</to>
        <from>...</from>
        <id>..</id>
        <relatesTo>...</relatesTo>
        <action>...</action>
        <version>...</version>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <customComplexElement>
            <a>a_v</a>
            <b>b_v</b>
            <c>c_v</c>
            <d>d_v</d>
            <e>e_v</e>
            <f>f_v</f>
        </customComplexElement>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我使用一种在线工具中生成了XSD文件:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
<import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"></import>
  <xs:element name="to" type="xs:string"/>
  <xs:element name="from" type="xs:string"/>
  <xs:element name="id" type="xs:string"/>
  <xs:element name="relatesTo" type="xs:string"/>
  <xs:element name="action" type="xs:string"/>
  <xs:element name="version" type="xs:string"/>
  <xs:element name="customComplexElement">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="a"/>
        <xs:element type="xs:string" name="b"/>
        <xs:element type="xs:string" name="c"/>
        <xs:element type="xs:string" name="d"/>
        <xs:element type="xs:string" name="e"/>
        <xs:element type="xs:string" name="f"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

然后,我使用wsdl2h.exe生成适当的标头文件,然后用SOAPCPP2.EXE编译器编译。然后,我尝试使用功能soap_read_customcomplexelement()读取XML文件,而我所得到的只是soap_tag_mismatch。如果我摆脱了所有肥皂的东西,这种方法似乎可以工作,但是我想知道GSOAP中是否有一些功能来解析肥皂信封,标头和身体?

我遇到了同样的问题。但是我只是修复了它。因此,就我而言,我使用此代码。

struct soap *soap = soap_new1(SOAP_C_UTFSTRING | SOAP_XML_IGNORENS | SOAP_XML_TREE);

密钥参数为 soap_xml_ignorens 。此参数忽略名称空间。

SOAP_XML_IGNORENS   in: ignores the use of XML namespaces in input

这个问题的根源是您不会在身体内容中声明名称空间。这就是为什么GSOAP不知道如何转换此XML。

这不会转换,因为GSOAP不知道什么是NS4:

<ns4:ParseKeywords><ns4:Keyword>Hello</ns4:Keyword></ns4:ParseKeywords>

,但是如果我声明名称空间,它将被转换

<ns4:ParseKeywords xmlns:ns4="com.idurdyev.idcommerce:ParseKeywords:1.0"><ns4:Keyword>Hello</ns4:Keyword></ns4:ParseKeywords>