JAXB - 解组XML异常 [英] JAXB - unmarshal XML exception

查看:121
本文介绍了JAXB - 解组XML异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过读一个大的xml文件(类似于500MB)。
首先,我使用xjc和我的XML的XSD文件。所有类都按预期生成。
尝试读取文件我遇到了这个错误:javax.xml.bind.UnmarshalException:意外元素。

I've tried to read a big xml file (something like 500MB). First of all, I used xjc with the XSD file of my XML. All classes were generated as expected. Trying to read the file I've got this error: javax.xml.bind.UnmarshalException: unexpected element.

这是我的代码:

(...)

JAXBContext context = JAXBContext.newInstance("br.com.mypackage");
Unmarshaller unmarshaller = context.createUnmarshaller();
File f = new File("src/files/MyHuge.CNX");
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
InputStream in = new FileInputStream(f);
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
Person p = null;
int count = 0;
while (eventReader.hasNext()) {
   XMLEvent event = eventReader.nextEvent();
   if (event.isStartElement()) {
      StartElement startElement = event.asStartElement();
      if (startElement.getName().getLocalPart() == ("person")) {
         p = (Person) unmarshaller.unmarshal(eventReader);
      }
   }
}

问题出在unmarshal上操作。

The problem is in the unmarshal operation.

Caused by: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"identification"). Expected elements are <{}messageAll>

我使用此链接作为示例来制作我自己的代码:JAXB - unmarshal OutOfMemory:Java堆空间

I used this link as example to make my own code: JAXB - unmarshal OutOfMemory: Java Heap Space

有人有这样做的线索?我现在想要的只是读取一个巨大的XML文件,而不用解组XML的外部对象(java堆空间问题),而无需通过标记读取标记获取相应的值,一个缓慢的猴子代码(不是行星的崛起的猴子)猿猴)。 :P

Someone has a clue to do it? All that I want now is to read a huge XML file without unmarshal the external object of XML (java heap space problem) and without reading tag by tag getting the respective value, a slow and monkey code (not the monkeys of Rise of the Planet of the Apes). :P

非常感谢。

推荐答案

我用以下代码解决了问题:

I solved the problem with this code bellow:

public List<Person> testeUnmarshal() {
  List<Person> people = new ArrayList<Person>();
  Person p = null;
  try {
    JAXBContext context = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    File f = new File(FILE_PATH);
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileInputStream(f));
    while (eventReader.hasNext()) {
      XMLEvent event = eventReader.peek();
      if (event.isStartElement()) {
        StartElement start = event.asStartElement();
    if (start.getName().getLocalPart() == "person")) {
          JAXBElement<Person> jax_b = unmarshaller.unmarshal(eventReader, Person.class);
      p = jax_b.getValue();
    }
      }
      eventReader.next();
    }
  } catch (Exception e) {
  }
  return persons;
}

我可以使用循环内的计数来控制内存中的对象数量(对于1000人在数据库中提交。)

I can control the amount of objects in memory using counts inside a loop (for 1000 Persons commit in database).

这篇关于JAXB - 解组XML异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆