如何“按原样”读写XML元素与JAXB? [英] How to read and write XML elements "as is" with JAXB?

查看:103
本文介绍了如何“按原样”读写XML元素与JAXB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须处理一些丑陋的XML片段,并且希望使用JAXB来节省一些时间。

I have to process some "ugly" XML pieces and would like to do so using JAXB to save some time.

假设我的文档结构如下:

Let's say my documents are structured like this:

<root a="1" b="2" c="3">
<header a="1" b="2" c="3">
<info/>
<more info/>
</header>
<body>lots of nested elements in here</body>
</root>

我想处理来自根元素和子元素的几个属性并将它们带入具有不同结构的新文档。问题是:我需要在我的新文档中保持 body 的内容不受影响。

I would like to process a couple of attributes from the root Element and the Children and bring them into a new document with a different structure. The problem is: I need to have the content of body untouched in my new document.

有没有办法(当解组/编组时)以按原样获取元素的内容?

Is there any way (when unmarshalling / marshalling) to take the content of an element "as is"?

推荐答案

可以将XML的一部分解组为一般 - 目的DOM对象,而不是JAXB生成的类。请查看此链接 this

It is possible to have parts of your XML unmarshalled into general-purpose DOM objects, rather than JAXB-generated classes. Take a look at this link and this one.

该示例适用于使用内联自定义绑定从W3C Schema生成Java类的时间。您可以将自定义绑定放在单独的文件中,并在生成Java类时使用XJC。

That example is for when you're generating your Java classes from a W3C Schema, using inline custom bindings. You can have the custom bindings in a separate file and have that used by XJC when generating the Java classes.

当然,如果您自己编写类,可以使用相同的使用正确的注释即可到达。

Naturally, in case you code the classes yourself, the same can be reached using the proper annotations.

这篇关于如何“按原样”读写XML元素与JAXB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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