简体   繁体   中英

Reading an XML file with JAXB

I can currently read the xml file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="100" r="q">
<datas>
    <data>
        <age>29</age>
        <name>mky</name>
    </data>
</datas>
</customer>

Using the Customer class:

@XmlRootElement
public class Customer {

String name;
String age;
String id;
String r;

@XmlAttribute
public void setR(String R) {
    this.r = R;
}   

    /etc
}

I decided to extend the XML file to support multiple customers:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customers>
<customer id="100" r="q">
        <age>29</age>
        <name>mky</name>
</customer>
<customer id="101" r="q">
        <age>29</age>
        <name>mky</name>
</customer>
</customers>

I then ran into some trouble trying to read this.

I tried adding a Customers class:

@XmlRootElement
public class Customers{
private ArrayList<Customer> customers;

public List<Customer> getCustomers() {
    return customers;
}

@XmlElement
public void setCustomers(ArrayList<Customer> customers) {
    this.customers = customers;
}

}

And then trying to print this with:

     try {

            File file = new File("/Users/s.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            Customers c = (Customers) jaxbUnmarshaller.unmarshal(file);

            System.out.println(c.getCustomers());

          } catch (JAXBException e) {
            e.printStackTrace();
          }

        }}

But I'm getting a null value for trying to print this. Can someone enlighten me on how I can read the second XML file?

Change your Customers class to

@XmlRootElement(name = "customers")
class Customers {
    private List<Customer> customers;

    public List<Customer> getCustomers() {
        return customers;
    }

    @XmlElement(name = "customer")
    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
}

What you don't want is a mismatch between the get/set methods for the XML element. If one is returning ArrayList , the other should accept an ArrayList argument. Similarly for List (which is just good practice).

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

  1. 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; } } 
  2. 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(); } } 

However note that annotations are required when you want to override default behavior. You find a full example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM