解组时为什么我会变空?

时间:2016-01-04 20:36:04

标签: java xml jaxb

我正在尝试使用JAXB读取XML文件,我也将它们放入列表中。

数据代码:

package xmll;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement(name="data")
public class data {

    private int millis;
    private String stamp;
    private String datetime;
    private int light;
    private double temp;
    private double vcc;


    @XmlElement
    public int getMillis() {
        return millis;
    }
    public void setMillis(int millis) {
        this.millis = millis;
    }

    @XmlElement
    public String getStamp() {
        return stamp;
    }
    public void setStamp(String stamp) {
        this.stamp = stamp;
    }

    @XmlElement
    public String getDatetime() {
        return datetime;
    }
    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }

    @XmlElement
    public int getLight() {
        return light;
    }
    public void setLight(int light) {
        this.light = light;
    }

    @XmlElement
    public double getTemp() {
        return temp;
    }
    public void setTemp(double temp) {
        this.temp = temp;
    }

    @XmlElement
    public double getVcc() {
        return vcc;
    }
    public void setVcc(double vcc) {
        this.vcc = vcc;
    }

    public data(int millis, String stamp, String datetime, int light, double temp, double vcc){
        super();
        this.millis = millis;
        this.stamp = stamp;
        this.datetime = datetime;
        this.light = light;
        this.temp = temp;
        this.vcc = vcc;
    }
    public data(){
        super();
    }

datalist code:

public class DataList {

    private List<data> listData = new ArrayList<data>();


    @XmlElement(name="Data")
    public List<data> getDataList(){
        return listData;
    }
    public void setListData(List<data> listData) {
        this.listData = listData;
    }
}

主要代码:

public class jaxb {
    public static void main(String[] args)throws JAXBException, FileNotFoundException {

        try {
            JAXBContext jc = JAXBContext.newInstance(DataList.class);
            Unmarshaller ums = jc.createUnmarshaller();
            DataList dl = (DataList)ums.unmarshal(new File(".\\data\fridgelogdata.xml"));

            System.out.println("the data bitch");
            for (data d: dl.getDataList()){
                System.out.println("millis=" + d.getMillis());
                System.out.println("stamp=" + d.getStamp());
                System.out.println("datetime=" + d.getDatetime());
                System.out.println("light=" + d.getLight());
                System.out.println("temp=" + d.getTemp());
                System.out.println("vcc=" + d.getVcc());    
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

问题出在我正在使用的实际xml文件中吗? 感谢任何帮助:)

这是xml的第一行,xml文件非常大,包含数千行,所以我无法显示整个文件。对任何初学者错误表示歉意。

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
     <row>
               <millis>1000</millis>
               <stamp> 1273010254</stamp>
               <datetime> 2010/5/4 21:57:34</datetime>
               <light> 333</light>
               <temp> 78.32</temp>
               <vcc> 3.54</vcc>
               </row>

1 个答案:

答案 0 :(得分:0)

您的xml 完全与数据和数据列表代码中的注释不同。

考虑xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
  <row>
     <millis>1000</millis>
     <stamp>1273010254</stamp>
     <datetime>2010/5/4 21:57:34</datetime>
     <light>333</light>
     <temp>78.32</temp>
     <vcc>3.54</vcc>
   </row>
</root>

正确的注释类可以是:

<强> Root.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
   "row"
})
@XmlRootElement(name = "root")
public class Root {

  @XmlElement(required = false)
  protected List<Row> row;

  public List<Row> getRow() {
    if (row == null) {
        row = new ArrayList<Row>();
    }
    return this.row;
  }

  @XmlAccessorType(XmlAccessType.FIELD)
  @XmlType(name = "", propOrder = {
    "millis",
    "stamp",
    "datetime",
    "light",
    "temp",
    "vcc"
  })
  public static class Row {
    protected Integer millis;
    protected String stamp;
    protected String datetime;
    protected Integer light;
    protected double temp;
    protected double vcc;

    public Integer getMillis() {
        return millis;
    }
    public void setMillis(Integer value) {
        this.millis = value;
    }
    public String getStamp() {
        return stamp;
    }
    public void setStamp(String value) {
        this.stamp = value;
    }
    //-...continue with the other getters and setters

  }
}

编辑按照评论

中的要求添加解组的方法
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller ums = jc.createUnmarshaller();
Root root = (Root)ums.unmarshal(new File("pathToYourFile"));
for (Root.Row r: root.getRow()){
  System.out.println("millis=" + r.getMillis());
  //ecc...
}