SAXReader reader = new SAXReader(); Document doc = reader.read(new FileInputStream("emplist.xml")); Element root = doc.getRootElement(); List<Element> list = root.elements("emp"); for(Element ele : list){ String name = ele.elementText("name"); int age = Integer.parseInt(ele.elementText("age")); System.out.println(name + " " + age);
package xml; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class XMLTest { public static void main(String[] args) { SAXReader reader = new SAXReader(); try { Document doc = reader.read(new FileInputStream("emplist.xml")); Element root = doc.getRootElement(); List<Element> emps = root.elements("emp"); for(Element ele : emps){ String name = ele.elementText("name"); int age = Integer.parseInt(ele.elementText("age")); System.out.println(name + ", " + age); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }