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

    代码运行前需要先导入dom4j架包。

    需要解析的XML文件test.xml如下:

    <students>  
        <student age="25"><!--如果没有age属性,默认的为20-->  
            <name>崔卫兵</name>  
            <college>PC学院</college>  
            <telephone>62354666</telephone>  
            <notes>男,1982年生,硕士,现就读于北京邮电大学</notes> 
        </student>  
        <student age="26">  
            <name>cwb</name>  
            <college leader="学院领导">PC学院</college><!--如果没有leader属性,默认的为leader--> 
            <telephone>62358888</telephone>  
            <notes>男,1987年生,硕士,现就读于中国农业大学</notes>  
        </student> 
    </students> 
    

    <1>、当测试文件test.xml在D盘时,Java程序代码如下:

    package Test01;
    
    import java.io.FileNotFoundException;
    import java.util.Iterator;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class test01 {
    
    
    	public void testRead() throws DocumentException, FileNotFoundException{
    		SAXReader reader=new SAXReader();
    
    		Document doc=reader.read("D:/test.xml");
    		Element root =doc.getRootElement();
    		for(@SuppressWarnings("rawtypes")
    		Iterator it=root.elementIterator();it.hasNext();){
    			Element element=(Element)it.next();
    
    			System.out.println(element.attribute("age").getName()+" == "+element.attribute("age").getValue());
    			System.out.println(element.attributeValue("age"));
    			System.out.println(element.getName());
    			for(@SuppressWarnings("rawtypes")
    			Iterator itt=element.elementIterator();itt.hasNext();){
    				//System.out.println(element.attributeValue("age"));
    				Element el=(Element)itt.next();
    				System.out.println(el.getName()+"=="+el.getText());  
    				//getText()获取的是两个标签间的数据如"<name>崔卫兵</name>"中的崔卫兵
    				//getName()获取的是标签名,即“<student age="25"><name>崔卫兵</name>”中的age和name
    				//attributeValue("age")可获取age的值即25
    			}
    			System.out.println("==================");			
    		}
    	}
    	
    	public static void main(String[] args){
    		try {
    			new test01().testRead();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} 
    	}
    }
    

    代码运行后控制台显示结果如下:

    <2>、当测试文件test.xml和项目在一起时,Java程序代码如下:

    package Test01;
    
    import java.io.FileNotFoundException;
    import java.util.Iterator;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class test01 {
    
    
    	public static void testRead(String path) throws DocumentException, FileNotFoundException{
    		SAXReader reader=new SAXReader();
    
    		//Document doc=reader.read("D:/test.xml");
    		Document doc = reader.read(path);
    		Element root =doc.getRootElement();
    		for(@SuppressWarnings("rawtypes")
    		Iterator it=root.elementIterator();it.hasNext();){
    			Element element=(Element)it.next();
    
    			System.out.println(element.attribute("age").getName()+" == "+element.attribute("age").getValue());
    			System.out.println(element.attributeValue("age"));
    			System.out.println(element.getName());
    			for(@SuppressWarnings("rawtypes")
    			Iterator itt=element.elementIterator();itt.hasNext();){
    				//System.out.println(element.attributeValue("age"));
    				Element el=(Element)itt.next();
    				System.out.println(el.getName()+"=="+el.getText());  
    				//getText()获取的是两个标签间的数据如"<name>崔卫兵</name>"中的崔卫兵
    				//getName()获取的是标签名,即“<student age="25"><name>崔卫兵</name>”中的age和name
    				//attributeValue("age")可获取age的值即25
    			}
    			System.out.println("==================");			
    		}
    	}
    	
    	public static void main(String[] args){
    //		try {
    //			new test01().testRead();
    //		} catch (Exception e) {
    //			// TODO Auto-generated catch block
    //			e.printStackTrace();
    //		} 
    		try {
    			testRead("test.xml");
    		} catch (FileNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (DocumentException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

    代码运行后控制台显示结果如下:

  • 相关阅读:
    初识云计算:历史、服务、架构
    云计算术语扫盲
    什么是 VxLAN?
    Linux用户态与内核态通信的几种方式
    Linux 命令多到记不住?这个开源项目帮你一网打尽!
    云计算时代,数据中心架构三层到大二层的演变
    Linux网络命令必知必会之瑞士军刀 nc(netcat)
    Docker 网络模型之 macvlan 详解,图解,实验完整
    基于alpine构建镜像报错temporary error (try again later)?
    win7环境下,vagrant,在启动虚拟机的时候报错io.rb:32:in `encode': incomplete "xC8" on GBK (Encoding::InvalidByteSequenceError)
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/3857873.html
Copyright © 2011-2022 走看看