如何将 xml 属性添加到 jaxb 带注释的类 XmlElementWrapper?
我有一个带有 XmlElementWrapper 注释的类,例如
......
@XmlElementWrapper(name="myList")
@XmlElements({
@XmlElement(name="myElement") }
)
private List<SomeType> someList = new LinkedList();
: 这段代码生成的 XML
<myList>
<myElement> </myElement>
<myElement> </myElement>
<myElement> </myElement>
</myList>
到目前为止一切顺利。
但现在我需要向列表标记添加属性以获取 XML,例如
<myList number="2">
<myElement> </myElement>
<myElement> </myElement>
<myElement> </myElement>
</myList>
是否有一种“智能方法可以实现此目的,而无需创建包含代表列表的新类?”
I have a class with a XmlElementWrapper annotation like:
...
@XmlElementWrapper(name="myList")
@XmlElements({
@XmlElement(name="myElement") }
)
private List<SomeType> someList = new LinkedList();
...
This code produces XML like
<myList>
<myElement> </myElement>
<myElement> </myElement>
<myElement> </myElement>
</myList>
so far so good.
But now I need to add attributes to the list tag to get XML like
<myList number="2">
<myElement> </myElement>
<myElement> </myElement>
<myElement> </myElement>
</myList>
Is there a 'smart way to achieve this without creating a new class that contains represents the list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我对你的问题有更好的解决方案。
要创建 Xml Java 对象,请使用以下代码:
}
要使用 JAXB 运行上述代码,请使用以下代码:
这将生成以下 XML 作为输出:
我认为这对您更有帮助。
谢谢..
I got a better solution for your question.
For making Xml Java object, use the following code:
}
To run the above code using JAXB, use the following:
This will produce the following XML as the output:
I think this is more helpful you.
Thanks..
MOXy JAXB 实现(我是技术主管)有一个扩展(@XmlPath) 来处理这种情况:
将生成以下 XML:
当此代码运行:
要使用严格标准的 JAXB 代码使其工作,您将需要使用 XML 适配器:
注意:
要使用 MOXy JAXB,您需要在模型类中添加一个名为 jaxb.properties 的文件,其中包含以下条目:
The MOXy JAXB implementation (I'm the tech lead) has an extension (@XmlPath) to handle this case:
Will produce the following XML:
When this code is run:
To get this to work using strictly standard JAXB code you will need to use an XML Adapter:
Note:
To use MOXy JAXB you need to add a file called jaxb.properties in with your model classes with the following entry:
如果您不使用 MOXy 或者只是想坚持使用标准 JAXB 注释,您可以扩展 Noby 的答案以添加对通用包装类的支持。诺比的答案目前仅支持字符串列表,但举例来说,您将为几个不同的类使用相同的通用包装类。在我的示例中,我想创建一个通用的“PagedList”类,它将编组为看起来像列表的内容,但还包含有关页面偏移量和未分页列表中元素总数的信息。
此解决方案的一个缺点是您必须为要包装的每种类型的类添加额外的 @XmlElement 映射。总的来说,这可能是比为每个可分页元素创建一个新类更好的解决方案。
编组这个例子会给你带来:
希望有帮助。这是我至少确定的解决方案。
If you are not using MOXy or just want to stick to standard JAXB annotations, you can extend upon Noby's answer to add support for a generic wrapper class. Noby's answer only currently supports a list of strings, but say for example you're going to be using the same generic wrapper class for several different classes. In my example, I want to create a generic "PagedList" class that will marshall to something that looks like a list, but also contains information about the page offset and the total number of elements in unpaged list.
The one downside of this solution is that you have to add additional @XmlElement mappings for each type of class that will be wrapped. Overall though, probably a better solution than creating a new class for each pagable elements.
Marshalling this example would get you:
Hope that helps. This is the solution that I settled upon at least.