如何在使用JAXB进行编组时包含soap信封标记? [英] How to include the soap envelope tag when marshalling using JAXB?

查看:60
本文介绍了如何在使用JAXB进行编组时包含soap信封标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am marshalling a soap request using JAXB. It is working but the resulting XML does not contain the soap:Envelope tag. Also, the namespace is indicated on root element instead of inside the soap:Envelope tag. There is also an additional standalone attribute on the xml tag. How can I achieved an output similar to the 2nd XML below using JAXB's marshaller?

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

Currently, here's how my marshalled XML looks like:

<?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>

以下是我希望它的样子:

And here is how I want it to look like:

<?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>


推荐答案

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

You can add your XML inside an Envelope before sending.

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

始终将您的命名空间保留在元素因为在查看元素的类型时你会清楚地知道它是什么类型。在你保留命名空间的位置并不重要。

Always keep your namespace at the element level; not at Envelope level. Because you get clarity when looking at an element what type it is. It doesn't matter where you keep the namespace.

有一个问题使用您的编组XML。正确的XML是:

There's a problem with your marshalled XML. Correct XML is:

<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>

再次,放置名称空间声明的位置无关紧要:

Again, it doesn't matter where you put the namespace declaration:

<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>

它们都是一样的。

这篇关于如何在使用JAXB进行编组时包含soap信封标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆