如何用JDOM快速定位到XML文件中的某一节点上?

解决方案 »

  1.   

    Element 类:(1)浏览Element树Element root = doc.getRootElement();//获得根元素elementList allChildren = root.getChildren();// 获得所有子元素的一个listList namedChildren = root.getChildren("name");// 获得指定名称子元素的listElement child = root.getChild("name");//获得指定名称的第一个子元素JDOM给了我们很多很灵活的使用方法来管理子元素(这里的List是java.util.List)
      

  2.   

    import org.jdom.output.*;import org.jdom.input.*;import org.jdom.*;import java.io.*;import java.util.*;public class ReadXML{    public static void main(String[] args) throws Exception {        SAXBuilder builder = new SAXBuilder();        Document read_doc = builder.build("studentinfo.xml");        Element stu = read_doc.getRootElement();        List list = stu.getChildren("student");        for(int i = 0;i < list.size();i++) {            Element e = (Element)list.get(i);            String str_number = e.getChildText("number");            String str_name = e.getChildText("name");            String str_age = e.getChildText("age");            System.out.println("---------STUDENT--------------");            System.out.println("NUMBER:" + str_number);            System.out.println("NAME:" + str_name);            System.out.println("AGE:" + str_age);            System.out.println("------------------------------");            System.out.println();        }         }}
      

  3.   

    上面的显示有点错误,这个程序是完整的import org.jdom.output.*;import org.jdom.input.*;import org.jdom.*;import java.io.*;import java.util.*;public class ReadXML{    public static void main(String[] args) throws Exception {        SAXBuilder builder = new SAXBuilder();        Document read_doc = builder.build("studentinfo.xml");        Element stu = read_doc.getRootElement();        List list = stu.getChildren("student");//这句话就是你想要的内容        for(int i = 0;i < list.size();i++) {            Element e = (Element)list.get(i);            String str_number = e.getChildText("number");            String str_name = e.getChildText("name");            String str_age = e.getChildText("age");            System.out.println("---------STUDENT--------------");            System.out.println("NUMBER:" + str_number);            System.out.println("NAME:" + str_name);            System.out.println("AGE:" + str_age);            System.out.println("------------------------------");            System.out.println();        }         }}