我正在使用 JAXB 编码 SOAP 请求。它正在工作,但生成的 XML 不包含 soap:Envelope
标记。此外,命名空间在根元素上指示,而不是在 soap:Envelope
标记内指示。 xml 标记上还有一个附加的 standalone
属性。如何使用 JAXB 的编码器获得类似于下面第二个 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>
最佳答案
您可以在发送之前将 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>
它们都是一样的。
关于java - 使用 JAXB 编码时如何包含 SOAP 信封标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425109/