如何在使用JAXB进行编组时包含soap信封标记?

时间:2012-07-11 03:26:24

标签: java xml soap jaxb marshalling

我正在使用JAXB编组soap请求。它正在工作,但生成的XML不包含soap:Envelope标记。此外,命名空间在根元素上指示,而不是在soap:Envelope标记内。 xml标记上还有一个额外的standalone属性。如何使用JAXB的marshaller实现类似于下面的第二个XML的输出?

目前,这是我编组的XML的样子:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Customer xmlns:ns="http://www.example.org/beanLevelNamespace">
    <ns:id>201200001</ns:id>
    <ns:name>Name</ns:name>
    <ns:age>18</ns:age>
</Customer>

以下是我希望它的样子:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
    soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">   
    <soap:Body xmlns:ns="http://www.example.org/beanLevel1Namespace" xmlns:ns1="http://www.example.org/beanLevel2Namespace">
        <ns:Customer>
            <ns1:id>201200001</ns:id>
            <ns1:name>Name</ns:name>
            <ns1:age>18</ns:age>
        </ns:Customer>
    </soap:Body>
</soap:Envelope>

1 个答案:

答案 0 :(得分:1)

您可以在发送之前在信封内添加XML。

"<Envelope><Body>" + your_xml + "</Body></Envelope>

始终将命名空间保留在元素级别;不在信封级别。因为在查看元素时它会变得清晰。保留命名空间的位置无关紧要。

您的编组XML存在问题。正确的XML是:

<ns:Customer xmlns:ns="http://www.example.org/beanLevelNamespace">
    <ns:id>201200001</ns:id>
    <ns:name>Name</ns:name>
    <ns:age>18</ns:age>
</ns:Customer>

同样,在命名空间声明的位置无关紧要:

<ns:Customer xmlns:ns="http://www.example.org/beanLevelNamespace">
    <ns:id>201200001</ns:id>
</ns:Customer>

<Customer xmlns="http://www.example.org/beanLevelNamespace">
    <id>201200001</id>
</Customer>

<ns1:Customer xmlns:ns1="http://www.example.org/beanLevelNamespace">
    <ns2:id xmlns:ns2="http://www.example.org/beanLevelNamespace">201200001</ns2:id>
</ns1:Customer>

他们都是一样的。