有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java SOAP请求xml内容作为字符串:prolog中不允许内容,并且文件过早结束错误

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

客户工作得很好。我可以调用大多数方法,得到的响应也很好,这意味着像请求、响应和名称空间这样的东西都很好。然而,有一种方法我遇到了麻烦。其请求数据类型的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,然后发送HTTPPOST请求。我得到的错误字符串是:Validation Error(s): Content is not allowed in prolog

我在谷歌上搜索,似乎Java无法识别第二行。我试图删除该行,但得到了另一个错误:premature end of file

我的问题是:我是否正确地放置了xml内容部分?是什么导致了这个错误?我怎样才能修好它


共 (1) 个答案

  1. # 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>
    

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