【问题标题】:SOAP request xml content as a string: Content is not allowed in prolog and Premature end of file errorSOAP 请求 xml 内容作为字符串:序言中不允许内容和文件过早结束错误
【发布时间】:2016-09-28 11:34:51
【问题描述】:

我正在尝试访问数据供应商的 SOAP Web 服务。服务器是用 Java 编写的。我正在从 Perl 访问它。由于某些特殊性,我必须自己发送原始 http 请求,而不是使用 SOAP Lite 包。

客户端工作正常。我可以调用大多数方法,而且我得到的响应看起来也不错,这意味着请求、响应和命名空间之类的东西都很好。但是,有一种方法我遇到了麻烦。其请求数据类型的wsdl如下:

<xs:element name="method1">
    <xs:complexType>
        <xs:sequence>
            <xs:element minOccurs="0" name="xmlContent" nillable="true" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

此方法的独特之处在于它接受一个字符串元素 xmlContent,它实际上是 xml 内容。这是我的要求:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
    <namesp1:method1 xmlns:namesp1="http://vendor.com">
    <xmlContent xsi:type="xsd:string">
        <?xml version="1.0" encoding="UTF-8"?>
        <product id="ABC" type="CSV">
        <columns>
            <column name="DATE" format="yyyymmdd" />
            <column name="COUNTRY" format="DESC"/>
        </columns>
        </product>
    </xmlContent>
    </namesp1:method1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我将请求保存在一个文件中,并将该文件读入 Perl,然后发送 http post 请求。我得到的故障字符串是:Validation Error(s): Content is not allowed in prolog。

我用谷歌搜索,似乎 Java 无法识别第二个 &lt;?xml version="1.0" encoding="UTF-8"?&gt; 行。我尝试删除该行,但出现不同的错误:premature end of file

我的问题:我是否以正确的方式放置 xml 内容部分?是什么导致了错误?我该如何解决?

【问题讨论】:

  • 第二个&lt;?xml ...&gt; 是无效的XML。

标签: java xml perl web-services soap


【解决方案1】:

xmlContent 元素的数据类型是xsd:string。这表明它应该是一个包含看起来像 XML 的内容的长字符串。将其命名为 xmlContent 是偶然的。他们只是随便取了个名字。

尝试传递一个包含 XML 的字符串,而不是一堆实际的 XML 标记。像这样使用CDATA 转义它。

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
    <namesp1:method1 xmlns:namesp1="http://vendor.com">
    <xmlContent xsi:type="xsd:string">
        <![CDATA[<?xml version="1.0" encoding="UTF-8"?>
        <product id="ABC" type="CSV">
        <columns>
            <column name="DATE" format="yyyymmdd" />
            <column name="COUNTRY" format="DESC"/>
        </columns>
        </product>]]>
    </xmlContent>
    </namesp1:method1>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

请注意,&lt;![CDATA[ 和 &lt;?xml&gt; 之间不应有换行符或其他空格,因为这将被视为内容的一部分,而另一端的服务器可能会在解析时阻塞部分作为实际的 XML。不过,&lt;![CDATA[ 前面的空格很好,因为它只是外部 XML 内部的任意空格,会被忽略。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-30
    • 2023-04-10
    • 2016-07-08
    • 2015-12-22
    • 1970-01-01
    • 2019-01-01
    相关资源
    最近更新 更多