Search code examples
javaxmljaxbunmarshallingstax

Unable to unmarshal the XML to my class using the 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.

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>

Following is the Customer.class which will be used for unmarshalling:

@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;
}

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);
      }
    }
  }
}

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?


Solution

  • 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();
          }
        }
      }
    }