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

发布于 2024-10-03 02:39:29 字数 1165 浏览 5 评论 0原文

我几乎正在尝试归档 如何在 Java 中将简单类序列化反序列化至 xml 并返回 (C#)。如果可能的话,我想避免为每个类编写序列化/反序列化方法。

例如,序列化的一部分:

    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.");
        }
    }

不是这个问题的一部分,因为我正在尝试一一解决问题,但可能会让您了解我正在尝试做什么。当我反序列化时,我还想检查输入是否有效。最终我想使用具有序列化形式的 XSLT 转换。

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. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这个俗人 2024-10-10 02:39:29

JAXB 是您将 Java 对象序列化为 XML 的方式。以下内容将帮助您入门:

JAXB 实现

此标准有多种实现:

Woodstox StAX 2

JAXB 接受许多输入/输出格式,包括 StAX。

验证

使用 Unmarshaller 将 XML 转换为对象,并使用 Marshaller 将对象转换为 XML。您可以设置 javax.xml.validation.Schema 的实例来验证这些操作期间的输入。

您还可以直接将 javax.xml.validation API 与 JAXB 结合使用,请查看以下示例:

XSLT

javax.xml.transform 库在 Java 中用于执行 XSLT 转换。 JAXB 旨在使用 JAXBSource 和 JAXBResult 与这些库配合使用。

了解更多信息

查看我的博客:

JAXB is how you serialize Java objects to XML. The following will help you get started:

JAXB Implementations

There are several implementations of this standard:

Woodstox StAX 2

JAXB accepts many input/output formats including StAX.

Validation

XML is converted to objects using an Unmarshaller, and objects are converted to XML with a Marshaller. You can set an instance of javax.xml.validation.Schema to validate the input during these operations.

You can also use the javax.xml.validation APIs directly with JAXB, check out the following for an example:

XSLT

The javax.xml.transform libraries are used in Java to perform XSLT transforms. JAXB is designed to work with these libraries using JAXBSource and JAXBResult.

For More Information

Check out my blog:

忆梦 2024-10-10 02:39:29

除了全面接受的答案之外,值得注意的是 Woodstox(或任何 Stax2 实现)实际上可以验证输入和输出;有关示例代码,请参阅此博客条目。好处之一是您还可以针对 Relax NG(JAXB 默认使用的 JAXP 解析器不支持 AFAIK)或 DTD 进行验证。

另外:有一个名为 Jackson-xml-databinder 的新项目( Jackson JSON 处理器),使用 Stax2 解析器(如 Woodstox 或 Aalto)实现“迷你 JAXB”(完整 JAXB 功能的子集)。主要优点是比 JAXB 实现更强大的数据绑定部分以及更好的性能;缺点是它不够成熟,并且不支持所有 XML 特定方面。在同时支持 JSON 和 XML 格式的情况下,它可能最有用。

In addition to the comprehensive accepted answer, it's worth noting that Woodstox (or any Stax2 implementation) can actually validate both input and output; 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.

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). Main benefits are bit more powerful data binding part and even better performance than JAXB implementations; downside that it is not as mature, and does not support all XML specific aspects. It is probably most useful in cases where both JSON and XML formats are to be supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文