EclipseLink MOXy适用于可笑的巨大XML文件吗?

时间:2012-11-15 11:46:07

标签: java xml jaxb eclipselink moxy

我在EclipseLink MOXy上花了一些愉快的时间,找出了在Spring中解析XML到POJO的最佳方法。我现在已经获得了一些XML来解析,文件的大小达到了令人难以置信的750MiB。

EclipseLink MOXy是否在下面使用流媒体技术,还是会尝试将整个文档保存在内存中?

2 个答案:

答案 0 :(得分:4)

注意:我是EclipseLink JAXB (MOXy)主管,是JAXB (JSR-222)专家组的成员。

尽可能EclipseLink JAXB (MOXy)利用StAX XMLStreamReader来处理XML输入。这意味着文档永远不会保存在内存中。

答案 1 :(得分:3)

我无法评论MOXy与任何其他JAXB实现,但是根据XML文件的结构和它们包含的数据类型,您可能需要考虑除解组整个XML文件的明显方法之外的其他方法在前面对象然后操纵它们。例如,如果非常大的文件包含许多小段

<root>
  <record>
    <id>1</id>
    <name>Ian</name>
  </record>
  <record>
    <id>2</id>
    <name>Deejay</name>
  </record>
  <!-- 100,000 more <record> elements -->
</root>

您可以使用

之类的内容单独处理每个细分
XMLInputFactory xif = XMLInputFactory.newFactory();
XMLStreamReader xsr = xif.createXMLStreamReader(inputStream);
JAXBContext ctx = JAXBContext.newInstance("com.example");
Unmarshaller um = ctx.createUnmarshaller();
xsr.nextTag(); // move to the <root> tag
xsr.nextTag(); // move to the first <record>

// read one <record> at a time
JAXBElement<Record> rec = um.unmarshal(xsr, Record.class);
// leaves the xsr pointing to the token after the </record> tag
// so you can do something with this Record, then discard it and
// parse the next...