简单解析自己定义的xml文件,首先,给解析xml文件的类来个构造方法:
public xmlData(File xmlFile) {
file = xmlFile;
try {
saxReader = new SAXReader();
saxReader.setEncoding("UTF-8");
doc = saxReader.read(this.file); init();
} catch (DocumentException e) {
throw new Exception("Open xml file " + xmlFile.getAbsolutePath()
+ " failed with error:" + e.getMessage()); }
}
下面是初始化数据的方法:
private void init() throws Exception {
try {
Node node = doc.selectSingleNode("//Com/User");
user = node.valueOf("@User");
name = node.valueOf("@Name");
}catch(Exception e){
throw new Exception("init xml file fail"+e.getMessage());
} }