如何用JDOM快速定位到XML文件中的某一节点上? 如何用JDOM快速定位到XML文件中的某一节点上? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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) 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(); } }} 上面的显示有点错误,这个程序是完整的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(); } }} 编译错误,求真相 很好玩的问题,既然java中接口都这么出色了,为什么还是会在一定程度上用抽象类呢?大家讨论下吧! web start 启动的应用程序如何发送HTTP请求 急急急!!大家可不可以帮一下,快来不及了。。hibernate 自己写SQL 文 简单的jdk配置问题 关于Socket建立连接时,程序等待没有返回结果的问题 p4 1.8G 256M,建议用Jbuiler几呢? :)***我的《信息小屋》软件,自认为很独特,请各位大哥、大姐给个评价。:)*** sysbase数据库导入SQLserver数据库 明天考scjp 又散分了 swt中的Table怎么选择单元格呢? 那位兄弟对Jdom操作XML比较熟悉,帮我解答一下。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货