简单解析自己定义的xml文件,首先,给解析xml文件的类来个构造方法:
1 public xmlData(File xmlFile) { 2 file = xmlFile; 3 try { 4 saxReader = new SAXReader(); 5 saxReader.setEncoding("UTF-8"); 6 doc = saxReader.read(this.file); 7 8 init(); 9 } catch (DocumentException e) { 10 throw new Exception("Open xml file " + xmlFile.getAbsolutePath() 11 + " failed with error:" + e.getMessage()); } 12 }
下面是初始化数据的方法:
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()); } }
时间: 2024-10-26 17:59:57