package OperateXMLFile; import java.io.File; import java.io.FileReader; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.input.SAXBuilder; public class ReadXMLFile { public String readXml(String filepath) { File file = new File(filepath); if(!file.exists()) { return "file not exist!"; } FileReader fi = null; try { fi = new FileReader(file); SAXBuilder sb = new SAXBuilder(); Document doc = sb.build(fi); Element root = doc.getRootElement(); // 得到根元素 List books = root.getChildren(); // 得到根元素所有子元素的集合 Element userName = null; Element password = null; Element nba = null; userName = (Element) books.get(0); password = (Element) books.get(1); nba = (Element) books.get(2); String userName_string = userName.getValue(); String password_string = password.getValue(); String nba_string = nba.getValue(); System.out.println("userName is:"+userName_string); System.out.println("password is:"+password_string); System.out.println("nba is:"+nba_string); fi.close(); return "success"; } catch (Exception e) { System.err.println(e + "error"); } return null; } public static void main(String[] args) { String filepath = "C://temp//file.xml"; ReadXMLFile rxf = new ReadXMLFile(); String result = rxf.readXml(filepath); System.out.println("result is:"+result); } }