如何在java中生成一个大的(30MB+)xml文件?

发布于 2024-10-24 05:29:19 字数 145 浏览 9 评论 0原文

文件本身不是那么大,应该适合内存。但一旦将其与其他开销因素结合起来,问题就开始出现。我们正在内存中构建 DOM,这对我们来说是无法扩展的。使用原始输出流似乎有问题,因为我们必须小心转义字符。

有哪些好的方法可以做到这一点?

有这方面的商品库吗?

The file itself is not that big, should fit in memory. But once you combine that with other overhead factors then starts to become a problem. We are building a DOM in memory and that is not scaling for us. Using raw output streams seems problematic in the sense that we have to be careful about escaping characters.

What are some good approaches for doing this?

Are there any goods libs for this?

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

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

发布评论

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

评论(4

你是暖光i 2024-10-31 05:29:19

STAX 提供了一个方便的 API,用于将 XML 写入输出流。简单教程此处

STAX provides a convenient API with which to write XML to output stream. Easy tutorial here.

彼岸花似海 2024-10-31 05:29:19

尝试 XStream

功能

  • 易于使用。提供了一个高级外观来简化常见用例。
  • 无需映射。大多数对象都可以序列化,无需指定映射。
  • 表现。速度和低内存占用是设计的关键部分,使其适合大型对象图或具有高消息吞吐量的系统。
  • 干净的 XML。没有可以通过反射获得的重复信息。这使得 XML 比本机 Java 序列化更容易阅读并且更紧凑。
  • 不需要对对象进行任何修改。序列化内部字段,包括私有字段和最终字段。支持非公共类和内部类。类不需要有默认构造函数。
  • 完整的对象图支持。将维护对象模型中遇到的重复引用。支持循环引用。
  • 与其他 XML API 集成。通过实现接口,XStream 可以直接序列化到任何树结构(不仅仅是 XML)或从任何树结构(不仅仅是 XML)进行序列化。
  • 可定制的转换策略。可以注册策略,允许自定义特定类型如何表示为 XML。
  • 错误消息。当由于 XML 格式错误而发生异常时,系统会提供详细的诊断信息以帮助隔离和修复问题。
  • 替代输出格式。模块化设计允许其他输出格式。 XStream 目前附带 JSON 支持和变形。

Try XStream

Features

  • Ease of use. A high level facade is supplied that simplifies common use cases.
  • No mappings required. Most objects can be serialized without need for specifying mappings.
  • Performance. Speed and low memory footprint are a crucial part of the design, making it suitable for large object graphs or systems with high message throughput.
  • Clean XML. No information is duplicated that can be obtained via reflection. This results in XML that is easier to read for humans and more compact than native Java serialization.
  • Requires no modifications to objects. Serializes internal fields, including private and final. Supports non-public and inner classes. Classes are not required to have default constructor.
  • Full object graph support. Duplicate references encountered in the object-model will be maintained. Supports circular references.
  • Integrates with other XML APIs. By implementing an interface, XStream can serialize directly to/from any tree structure (not just XML).
  • Customizable conversion strategies. Strategies can be registered allowing customization of how particular types are represented as XML.
  • Error messages. When an exception occurs due to malformed XML, detailed diagnostics are provided to help isolate and fix the problem.
  • Alternative output format. The modular design allows other output formats. XStream ships currently with JSON support and morphing.
入画浅相思 2024-10-31 05:29:19

通过 Saxon,您可以将 StAX XMLStreamWriter API 与序列化器结合使用,序列化器使您可以完全控制 xsl:output 中定义的序列化属性,例如控制缩进、CDATA 部分的使用等的能力。请参阅 s9api 序列化器班级。

With Saxon, you can use the StAX XMLStreamWriter API in conjunction with a Serializer that gives you full control of the serialization properties as defined in xsl:output, for example the ability to control indentation, use of CDATA sections, etc. See the s9api Serializer class.

原谅我要高飞 2024-10-31 05:29:19

这取决于您的数据的结构,但 StAX 实现可能就是您正在寻找的 - 例如 Woodstock。

It depends on how your data is structured, but a StAX implementation might be what you are looking for - like Woodstock for example.

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