为每个元素生成带有命名空间的 XML
如何使用 JAXB 生成具有以下架构的 XML。
<NS1:getRatesResponse xmlns:NS1="http://mynamespaceTypes">
<response>
<NS2:rates xmlns:NS2="http://mynamespace">
<currency>USD</currency>
</NS2:rates>
<NS3:rates xmlns:NS3="http://mynamespace">
<currency>EUR</currency>
</NS3:rates>
<NS4:rates xmlns:NS4="http://mynamespace">
... etc
</response>
</NS1:getRatesResponse>
我不知道如何告诉 JAXB 每个新项目都应该是具有相同命名空间的 NS(n+1) 。更改 XML 格式不是一个选项,因为它是外部的。
JAXB 正确解析此 XML,但是当使用相同的类生成时,它会生成如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:getRatesResponse
xmlns:ns2="http://mynamespaceTypes"
xmlns:ns3="http://mynamespace">
<response>
<ns2:rates>
<currency>EUR</currency>
</ns2:rates>
<ns2:rates>
<currency>USD</currency>
</ns2:rates>
</response>
</ns3:getRatesResponse>
How can I generate XML with the following schema using JAXB.
<NS1:getRatesResponse xmlns:NS1="http://mynamespaceTypes">
<response>
<NS2:rates xmlns:NS2="http://mynamespace">
<currency>USD</currency>
</NS2:rates>
<NS3:rates xmlns:NS3="http://mynamespace">
<currency>EUR</currency>
</NS3:rates>
<NS4:rates xmlns:NS4="http://mynamespace">
... etc
</response>
</NS1:getRatesResponse>
I don't know how to tell JAXB that every new item should be NS(n+1) with the same namespace. Changing XML format is not an option, because it's external.
JAXB parses this XML correctly, but when producing using same classes it produces it like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns3:getRatesResponse
xmlns:ns2="http://mynamespaceTypes"
xmlns:ns3="http://mynamespace">
<response>
<ns2:rates>
<currency>EUR</currency>
</ns2:rates>
<ns2:rates>
<currency>USD</currency>
</ns2:rates>
</response>
</ns3:getRatesResponse>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于此用例,我将执行以下操作:
XMLStreamWriter
getRatesResponse
和response
元素直接写入XMLStreamWriter
code>marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
上设置以下属性,以防止在每次编组调用时写入标头。Rate
对象分别编组到XMLStreamWriter
。NamespacePrefixMapper
实例来控制命名空间前缀(这当前需要 JAXB RI,目前正在将对此扩展的支持添加到 EclipseLink JAXB (MOXy))。了解更多信息
For this use case I would do the following:
XMLStreamWriter
getRatesResponse
andresponse
elements directly to theXMLStreamWriter
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
to prevent the header from being written on each marshal call.Rate
objects to theXMLStreamWriter
individually.NamespacePrefixMapper
on it to control the namespace prefix (this currently requires the JAXB RI, support for this extension is currently being added to EclipseLink JAXB (MOXy)).For More Information
将这些配置添加到我的方案中解决了我的问题
attributeFormDefault="不合格" elementFormDefault="合格"
Adding these config to my scheme solved my issue
attributeFormDefault="unqualified" elementFormDefault="qualified"