Rectangle 27 0

java Reading an XML file with JAXB?

However note that annotations are required when you want to override default behavior.

  • First delete any annotation in your Customers class public class Customers{ private ArrayList<Customer> customers; public List<Customer> getCustomers() { return customers; } public void setCustomers(ArrayList<Customer> customers) { this.customers = customers; } }
  • Second use an instance of JAXBElement in your parsing method try { File file = new File("/Users/s.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); JAXBElement<Customers> je1 = unmarshaller.unmarshal(file, Customers.class); Customers c = je1.getValue(); System.out.println(c.getCustomers()); } catch (JAXBException e) { e.printStackTrace(); } }

If you have problems using annotations, it is possible to remove them and use an instance of JAXBElement instead. To do so: