【问题标题】:Is EclipseLink MOXy suitable for ridiculously huge XML files?EclipseLink MOXy 是否适用于异常巨大的 XML 文件?
【发布时间】:2012-11-15 11:46:07
【问题描述】:

我在 EclipseLink MOXy 上度过了一段愉快的时光,寻找在 Spring 中将 XML 解析为 POJO 的最佳方法。我现在得到了一些要解析的 XML,文件的大小达到了令人难以置信的 750MiB。

EclipseLink MOXy 是在底层使用流技术,还是会尝试将整个文档保存在内存中?

【问题讨论】:

    标签: java xml jaxb eclipselink moxy


    【解决方案1】:

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

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

    【讨论】:

    • Blaise - 我一直在使用外部元数据配置。关于如何在单个文件中“迭代”多个记录并在每个记录被解析时触发某种事件的任何建议?否则,虽然 XML 不会保存在内存中,但 POJO 表示将立即全部在堆中。
    • @Deejay - 我认为您正在寻找 Ian Roberts 建议的方法:stackoverflow.com/a/13397375/383861
    【解决方案2】:

    我无法评论 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...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2015-09-20
      相关资源
      最近更新 更多