<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Row>
<MyDataSource>
<driverClassName></driverClassName>
<url></url>
<userName></userName>
<password></password>
</MyDataSource>
<MySourceSql>
</MySourceSql>
<TerminalSource>
<driverClassName></driverClassName>
<url></url>
<userName></userName>
<password></password>
</TerminalSource>
<TerminaSql>
</TerminaSql>
</Row>
</Data>
上面是创建的xml文件,标签里面可以随便写值。
然后是创建model类;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Data")
public class Data {
@XmlElement(name = "Row")
private List<Row> rowList;
@Override
public String toString() {
return "Data{" +
"rowList=" + rowList +
'}';
}
public List<Row> getRowList() {
return rowList;
}
public void setRowList(List<Row> rowList) {
this.rowList = rowList;
}
}
意思就是,data是一个类,它包含row,row又包含下面的,我这个row以后可能会加多个属性,所以打data里面我写的是链表,依次建好类就行了,代码我就不一个一个贴出来了。
File file = new File("D:\\code\\bocPayPullSettleLs\\src\\main\\resources\\applicationinputSet.xml");
JAXBContext jaxbC = JAXBContext.newInstance(Data.class);
Unmarshaller us = jaxbC.createUnmarshaller();
Data data=(Data)us.unmarshal(file);
然后读取就可以了,就会返回个data类。