xml文件Str转换成Object对象
作者:互联网
一、xml格式
<?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>
@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() {
eturn rowList;
}
public void setRowList(List<Row> rowList) {
this.rowList = rowList;
}
}
二、
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public static Object convertXmlStrToObject(Class clazz, String xmlStr) throws Exception {
Object xmlObject = null;
JAXBContext context = JAXBContext.newInstance(clazz);
// 进行将Xml转成对象的核心接口
Unmarshaller unmarshaller = context.createUnmarshaller();
StringReader sr = new StringReader(xmlStr);
xmlObject = unmarshaller.unmarshal(sr);
return xmlObject;
}
标签:xml,rowList,Object,javax,Str,xmlObject,public 来源: https://www.cnblogs.com/liveinpresent/p/11627493.html