【问题标题】:Parse soap msg with gSOAP使用 gSOAP 解析肥皂味精
【发布时间】:2017-02-16 09:53:12
【问题描述】:

我有以下示例 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 中是否有一些函数可以解析肥皂信封、标题和正文?

【问题讨论】:

    标签: c++ soap gsoap


    【解决方案1】:

    我也遇到了同样的问题。但我刚刚修好了。 所以,就我而言,我使用这行代码。

    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>
    

    【讨论】:

      猜你喜欢
      • 2013-10-06
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多