zoukankan      html  css  js  c++  java
  • dom4j解析xml实例(2)

    dom4j是一个java的XML API,类似jdom,用来读写XML文件,它性能优异、功能强大和极易使用等特点

    所用jar包:dom4j-1.6.1.jar、jaxen-1.1-beta-6.jar

    需要解析的xml文件:people.xml

    <people city="shenzhen">  
        <student name="milton" age="22"></student>  
        <student name="lego" age="23"></student>  
        <teacher name="bruce" age="27"></teacher>  
        <teacher name="ziven" age="28"></teacher>  
    </people>
    

    java代码如下:

    package demo5;
    
    import java.io.File;
    import java.util.Iterator;
    import java.util.List;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
      
    public class Test01 {
        public static void main(String args[]) throws DocumentException {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("D:/people.xml"));
            Element rootElm = document.getRootElement();
            //Element root1Elm = rootElm.element("city");
            @SuppressWarnings("rawtypes")
            List nodes = rootElm.elements("student");
            @SuppressWarnings("rawtypes")
    		List nodess = rootElm.elements("teacher");
            for (@SuppressWarnings("rawtypes")
            Iterator it = nodes.iterator(); it.hasNext();) {
                Element elm = (Element) it.next();
                System.out.println("name:" + elm.attributeValue("name")
                        + " age:" + elm.attributeValue("age"));
            }
            for (@SuppressWarnings("rawtypes")
            Iterator it = nodess.iterator(); it.hasNext();) {
                Element elm = (Element) it.next();
                System.out.println("name:" + elm.attributeValue("name")
                        + " age:" + elm.attributeValue("age"));
            }
            System.out.println();
            try {
                Document doc = reader.read(new File("D:/people.xml"));
                @SuppressWarnings("rawtypes")
                List projects = doc.selectNodes("people/student");
                @SuppressWarnings("rawtypes")
    			List projectss = doc.selectNodes("people/teacher");
                @SuppressWarnings("rawtypes")
                Iterator it = projects.iterator();
                while (it.hasNext()) {
                    Element elm = (Element) it.next();
                    System.out.println("name:" + elm.attributeValue("name")
                            + " age:" + elm.attributeValue("age"));
                }
                @SuppressWarnings("rawtypes")
                Iterator its = projectss.iterator();
                while (its.hasNext()) {
                    Element elm = (Element) its.next();
                    System.out.println("name:" + elm.attributeValue("name")
                            + " age:" + elm.attributeValue("age"));
                }
      
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
      
    }
    

    代码运行后结果如下:

  • 相关阅读:
    免费公共dns推荐
    vm10 mac 序列号
    sublimetext 序列号
    操作系统第6次实验报告:使用信号量解决进程互斥访问
    操作系统第5次实验报告:内存管理
    操作系统第4次实验报告:文件系统
    操作系统第3次实验报告:管道
    操作系统第2次实验报告:创建进程
    OS第1次实验报告:熟悉使用Linux命令和剖析ps命令
    第四次实验报告:使用Packet Tracer理解RIP路由协议
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3875086.html
Copyright © 2011-2022 走看看