简体   繁体   English

用JAXB读取XML文件

[英]Reading an XML file with JAXB

I can currently read the xml file: 我目前可以读取xml文件:

<?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文件以支持多个客户:

<?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: 我尝试添加一个Customer类:

@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? 有人可以启发我如何读取第二个XML文件吗?

Change your Customers class to 将您的Customers类别更改为

@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. 您不希望XML元素的get / set方法之间不匹配。 If one is returning ArrayList , the other should accept an ArrayList argument. 如果一个返回ArrayList ,则另一个应接受ArrayList参数。 Similarly for List (which is just good practice). 对于List同样(这只是一个好习惯)。

If you have problems using annotations, it is possible to remove them and use an instance of JAXBElement instead. 如果您在使用批注时遇到问题,则可以将其删除并改用JAXBElement实例。 To do so: 为此:

  1. First delete any annotation in your Customers class 首先删除您的Customers类中的任何注释

     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 其次在解析方法中使用JAXBElement的实例

     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 . 您可以在此处找到完整的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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