无法使用 XMLEventReader 将 XML 解组到我的班级 [英] Unable to unmarshal the XML to my class using the XMLEventReader

查看:27
本文介绍了无法使用 XMLEventReader 将 XML 解组到我的班级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为大型 XML 文件执行 JAXB Unmarshalling,因此使用 XMLEventReader 但它没有按预期工作并且没有执行解组.

I am trying to perform JAXB Unmarshalling for a large XML file so using the XMLEventReader but it's not working as expected and not performing the Unmarshalling.

以下是我试图解组的XML文件:

Following is the XML file that I am trying to unmarshal:

<Customers>
  <Customer>
    <name>BATMAN</name>
    <age>2008</age>
  </Customer>
  <Customer>
    <name>SuperMan</name>
    <age>2022</age>
  </Customer>
</Customers>

以下是用于解组Customer.class:

@XmlRootElement(name = "Customer")
@XmlType(name = "Customer", propOrder = {"name", "age"})
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Customer {
  private String name;
  private String age;
}

以下是 Main 类,它将 unmarshal 每个 customer 并存储在 customer 字段中,但是它返回空值.通常在我希望它向我打印客户信息时.

Following is the Main class which will unmarshal each of the customer and store in the customer field but it returns null. Normally when I would expect it to print me the customer information.

class Main {

  public static void main(String[] args) throws JAXBException, XMLStreamException, JsonProcessingException {
    final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("Customer.xml");
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    final XMLEventReader streamReader = xmlInputFactory.createXMLEventReader(inputStream);
    final JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    while (streamReader.hasNext()) {
      final XMLEvent e = streamReader.nextEvent();

      if (e.isStartElement() && ((StartElement) e).getName().getLocalPart().equals("Customer")) {
        System.out.println(((StartElement) e).getName().getLocalPart());
        final Customer customer = unmarshaller.unmarshal(streamReader, Customer.class).getValue();
        System.out.println(customer);
      }
    }
  }
}

我尝试使用 XMLStreamReader 并且效果很好.但我想利用 XMLEventReader peak() 功能.因此,我正在尝试使用 XMLEventReader.我查看了许多示例,他们提供了类似的示例,但不确定我的案例出了什么问题.

I tried with the XMLStreamReader and it works perfectly. But I want to make use of XMLEventReader peak() functionality. Hence, I am trying to make use of XMLEventReader. I looked at many examples and they provided similar examples not sure what's going wrong in my case.

有人能帮我理解这个问题吗?

Can someone please help me in understanding the issue?

推荐答案

在尝试了几件事情之后,我终于让它工作了.在这里发布答案,以便将来对某人有用:

After trying few things I was able to get it working. Posting the answer here so it can be useful to someone in the future:

class Main {

  public static void main(String[] args) throws JAXBException, XMLStreamException, JsonProcessingException {
    final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("Customer.xml");
    final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(inputStream);
    final JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    while (xmlEventReader.peek() != null) {
      XMLEvent event = xmlEventReader.peek();

      if (event.isStartElement() && ((StartElement) event).getName().getLocalPart().equals("Customer")) {
        Customer customer = unmarshaller.unmarshal(xmlEventReader, Customer.class).getValue();
        System.out.println("Normal Customer : " + customer);
      } else {
        xmlEventReader.next();
      }
    }
  }
}

这篇关于无法使用 XMLEventReader 将 XML 解组到我的班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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