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();
		}
	}
}

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

原文地址:https://www.cnblogs.com/henuyuxiang/p/3857873.html