让我说清楚: 你从一棵豆子树开始。您希望使用它们来构建一个遵循其结构但具有自己的语法/模式的 XML 文档,因此简单的 bean XML 序列化是不可能的...
如果是这样,那么 Blaise Doughan 推荐的 JAXB 是一个很好的建议。但是,如果您需要对 XML 格式进行更精细的控制,您需要执行一些高度特定的序列化,或者您可能希望在创建 XML 时删除一些 bean 引用以允许在执行期间进行垃圾收集,那么 Streaming API XML (StAX) 可能正是您所需要的。您可以使用它将 XML 构造写入流。
Let me get this straight: You start off with a tree of beans. You wish to use these to construct an XML document following their structure but with its own syntax/schema so simple bean XML serialization is out of the question...
If that is so, JAXB as recommended by Blaise Doughan is a good suggestion. If, however, you require a much finer control over the XML formatting, you need to do some highly specific serialization, or perhaps you wish to remove some bean references while creating the XML to allow garbage collection during the execution, then the Streaming API for XML (StAX) might be what you need. You can write XML constructs to a stream with it.
It will be difficult using JDOM, even if you want to dig in and write customized sub classes. XMLOutputter sort of assumes that it is given a complete tree that it can traverse. To save memory, you must somehow arrange so that only the current sub-tree exist. It is quite feasible to chop off the sub trees that has been traversed; but how do you postpone the creation of sub trees until they are needed. All this is going to require some kind of coordination between the sub classed XMLOutputter, your specialized Element and your bean navigation code. Probably not worth the effort.
I, as G_H also does, would recommend taking a look at Stax, javax.xml.stream.XMLStreamWriter in combination with your own "bean navigator".
Using EclipseLink JAXB (MOXy) you may be able to get the formatting you need. This will allow you to eliminate the JDOM piece in order to reduce the memory footprint.
Example
Assuming we have the following class in our model (accessors omitted to save space):
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {
private Address billingAddress;
private Address shippingAddress;
}
JAXB Mapping
Since EclipseLink MOXy is compliant with the JAXB specification (JSR-222) it will produce the following document by default:
发布评论
评论(5)
让我说清楚:
你从一棵豆子树开始。您希望使用它们来构建一个遵循其结构但具有自己的语法/模式的 XML 文档,因此简单的 bean XML 序列化是不可能的...
如果是这样,那么 Blaise Doughan 推荐的 JAXB 是一个很好的建议。但是,如果您需要对 XML 格式进行更精细的控制,您需要执行一些高度特定的序列化,或者您可能希望在创建 XML 时删除一些 bean 引用以允许在执行期间进行垃圾收集,那么 Streaming API XML (StAX) 可能正是您所需要的。您可以使用它将 XML 构造写入流。
http://download.oracle.com /javase/6/docs/api/javax/xml/stream/package-summary.html
抱歉,如果这不完全是您的意思。如果我理解错了,您能给我们一个小的使用场景吗?
Let me get this straight:
You start off with a tree of beans. You wish to use these to construct an XML document following their structure but with its own syntax/schema so simple bean XML serialization is out of the question...
If that is so, JAXB as recommended by Blaise Doughan is a good suggestion. If, however, you require a much finer control over the XML formatting, you need to do some highly specific serialization, or perhaps you wish to remove some bean references while creating the XML to allow garbage collection during the execution, then the Streaming API for XML (StAX) might be what you need. You can write XML constructs to a stream with it.
http://download.oracle.com/javase/6/docs/api/javax/xml/stream/package-summary.html
Sorry if it isn't exactly what you meant. If I'm getting it wrong, could you give us a small usage scenario?
您是否尝试过 Xstream,一个 xml 序列化库?
Have you tried Xstream, a xml serialization library?
您还可以尝试 http://commons.apache.org/betwixt/
You can also try http://commons.apache.org/betwixt/
即使您想深入研究并编写定制的子类,使用 JDOM 也会很困难。 XMLOutputter 有点假设给它一个可以遍历的完整树。为了节省内存,您必须以某种方式进行安排,以便仅存在当前子树。砍掉已经遍历过的子树是相当可行的;但是如何推迟子树的创建直到需要它们为止。所有这些都需要子类 XMLOutputter、专用元素和 bean 导航代码之间进行某种协调。可能不值得付出努力。
我,就像 G_H 一样,建议结合您自己的“bean 导航器”查看 Stax、javax.xml.stream.XMLStreamWriter。
It will be difficult using JDOM, even if you want to dig in and write customized sub classes. XMLOutputter sort of assumes that it is given a complete tree that it can traverse. To save memory, you must somehow arrange so that only the current sub-tree exist. It is quite feasible to chop off the sub trees that has been traversed; but how do you postpone the creation of sub trees until they are needed. All this is going to require some kind of coordination between the sub classed XMLOutputter, your specialized Element and your bean navigation code. Probably not worth the effort.
I, as G_H also does, would recommend taking a look at Stax, javax.xml.stream.XMLStreamWriter in combination with your own "bean navigator".
偏见声明 - 我是 EclipseLink JAXB (MOXy)
使用 EclipseLink JAXB (MOXy) 您也许能够获得所需的格式。这将允许您消除 JDOM 部分以减少内存占用。
示例
假设我们的模型中有以下类(省略访问器以节省空间):
JAXB 映射
自从 EclipseLink MOXy 符合 JAXB 规范 (JSR-222) 它将默认生成以下文档:
路径映射
如果您需要细粒度控制在 XML 上,您可以使用 MOXy 的 @XmlPath 扩展。
按如下方式注释字段:
将导致生成以下 XML:
位置映射
XPath 片段可以包含位置指示符:
生成的 XML 将如下所示:
条件映射
生成的 XML 将为:
了解更多信息
Declaration of Bias - I'm the EclipseLink JAXB (MOXy) lead
Using EclipseLink JAXB (MOXy) you may be able to get the formatting you need. This will allow you to eliminate the JDOM piece in order to reduce the memory footprint.
Example
Assuming we have the following class in our model (accessors omitted to save space):
JAXB Mapping
Since EclipseLink MOXy is compliant with the JAXB specification (JSR-222) it will produce the following document by default:
Path Mapping
If you require fine grain control over your XML you can use MOXy's @XmlPath extension.
Annotating the fields as follows:
Will cause the following XML to be produced:
Positional Mapping
The XPath fragments can include a positional indicator:
The resulting XML will look like:
Conditional Mapping
And the resulting XML will be:
For More Information