【发布时间】:2014-10-14 12:04:03
【问题描述】:
我有一个这样的 XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set>
<record>
<TARIH>data</TARIH>
<GUNLER>data</GUNLER>
<YEMEK1>data</YEMEK1>
<YEMEK2>data</YEMEK2>
</record>
<record>
<TARIH>data</TARIH>
<GUNLER>data</GUNLER>
<YEMEK1>data</YEMEK1>
<YEMEK2>data</YEMEK2>
</record>
</data-set>
我想用 Java 中的 JAXB 解析它。这是我的 DataSet 类。
@XmlRootElement(name="data-set")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSet {
@XmlElement(name="record")
private List<Record> records = null;
public List<Record> getRecords(){
return records;
}
public void setRecords(List<Record> records){
this.records = records;
}
}
这是我的 Record 类。
@XmlRootElement(name="record")
@XmlAccessorType(XmlAccessType.FIELD)
public class Record {
String TARIH,GUNLER,YEMEK1,ANAYEMEK1,ANAYEMEK2,YEMEK3,YEMEK4,SALATBAR1,SALATBAR2,SALATBAR3,SALATBAR4,SALATBAR5;
//getters and setters//
我尝试了类似的方法。
public class Main {
public static void main(String[] args) throws JAXBException {
File file = new File("C:/Users/EMRE/Desktop/YEMEKHANE DATABASE/morning.xml");
JAXBContext jaxbcontext = JAXBContext.newInstance(Record.class);
Unmarshaller jaxbunmarshaller = jaxbcontext.createUnmarshaller();
Record record = (Record)jaxbunmarshaller.unmarshal(file);
System.out.println(record.getTARIH());
}
}
我遇到了这样的错误。
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"data-set"). Expected elements are <{}record>
我该如何解决这个问题?谢谢。
【问题讨论】: