简体   繁体   English

如何使用Woodstox StAX 2序列化/反序列化XML中的类

[英]How do I serialize / deserialize a class in XML with Woodstox StAX 2

I'm pretty much trying to archive, what has been done in how-to-serialize-deserialize-simple-classes-to-xml-and-back (C#) in Java. 我正在尝试归档,在Java中如何对序列化 - 反序列化 - 简单类到xml-and-back (C#)所做的工作。 If possible, I would like to avoid writing a serialize / deserialize methods for each class. 如果可能的话,我想避免为每个类编写序列化/反序列化方法。

For example, part of serialize: 例如,序列化的一部分:

    XMLOutputFactory xof = null;
    XMLStreamWriter2 writer = null;

    try {
        resp.setContentType("text/plain");
        xof = XMLOutputFactory.newInstance();
        writer = (XMLStreamWriter2) //
        xof.createXMLStreamWriter(resp.getOutputStream());

        writer.writeStartDocument("1.0");
        writer.writeStartElement("data");
        // 
        // Magic happens here.
        //
        writer.writeEndElement();
        writer.writeEndDocument();
    } catch (XMLStreamException e) {
        e.printStackTrace();
        resp.sendError(1, "Problem 1 occured.");
    } finally {
        try {
            writer.flush();
            writer.close();
        } catch (XMLStreamException e) {
            e.printStackTrace();
            resp.sendError(2, "Problem 2 occured.");
        }
    }

Not part of this question, as I'm trying to tackle problems 1 by 1, but might give you a sense of what I'm trying to do. 不是这个问题的一部分,因为我试图逐一解决问题,但可能会让你了解我正在尝试做什么。 When I deserialize, I would also like to check if the input is valid. 当我反序列化时,我还想检查输入是否有效。 Eventually I want to use XSLT transforms with serialized form. 最终我想使用序列化形式的XSLT转换。

JAXB is how you serialize Java objects to XML. JAXB是将Java对象序列化为XML的方式。 The following will help you get started: 以下内容将帮助您入门:

JAXB Implementations JAXB实现

There are several implementations of this standard: 该标准有几种实现方式:

Woodstox StAX 2 Woodstox StAX 2

JAXB accepts many input/output formats including StAX. JAXB接受许多输入/输出格式,包括StAX。

Validation 验证

XML is converted to objects using an Unmarshaller, and objects are converted to XML with a Marshaller. 使用Unmarshaller将XML转换为对象,并使用Marshaller将对象转换为XML。 You can set an instance of javax.xml.validation.Schema to validate the input during these operations. 您可以设置javax.xml.validation.Schema的实例以在这些操作期间验证输入。

You can also use the javax.xml.validation APIs directly with JAXB, check out the following for an example: 您还可以直接使用JAXB使用javax.xml.validation API,请查看以下示例:

XSLT XSLT

The javax.xml.transform libraries are used in Java to perform XSLT transforms. Java中使用javax.xml.transform库来执行XSLT转换。 JAXB is designed to work with these libraries using JAXBSource and JAXBResult. JAXB旨在使用JAXBSource和JAXBResult来处理这些库。

For More Information 欲获得更多信息

Check out my blog: 看看我的博客:

In addition to the comprehensive accepted answer, it's worth noting that Woodstox (or any Stax2 implementation) can actually validate both input and output; 除了全面接受的答案之外,值得注意的是Woodstox(或任何Stax2实现)实际上可以验证输入和输出; see this blog entry for sample code. 请参阅此博客条目以获取示例代码。 One benefit is that you can also validate against Relax NG (not supported AFAIK by JAXP parser that JAXB uses by default) or DTD. 一个好处是您还可以验证Relax NG(默认情况下JAXB使用的JAXP解析器不支持的AFAIK)或DTD。

Also: there is a new project called Jackson-xml-databinder (a spin-off of Jackson JSON processor) that implements "mini-JAXB" (subset of full JAXB functionality) using a Stax2 parser (like Woodstox or Aalto). 另外:有一个名为Jackson-xml-databinder的新项目(Jackson JSON处理器的衍生产品),它使用Stax2解析器(如Woodstox或Aalto)实现“mini-JAXB”(完整JAXB功能的子集)。 Main benefits are bit more powerful data binding part and even better performance than JAXB implementations; 主要优点是比JAXB实现更强大的数据绑定部分甚至更好的性能; downside that it is not as mature, and does not support all XML specific aspects. 缺点是它不够成熟,并且不支持所有XML特定方面。 It is probably most useful in cases where both JSON and XML formats are to be supported. 在支持JSON和XML格式的情况下,它可能最有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM