SAX解析XML文件

/*
SAX與DOM解析XML的區別:

解析xml有四種方法:DOM,SAX,DOM4j,JDOM.
     我們主要學了兩種:DOM和SAX.
     DOM適於解析比較簡單的XML而SAX則適於解析較複雜的XML文件。各有各的好。

     DOM和SAX的不同:
     1. DOM是基於內存的,不管文件有多大,都會將所有的內容預先裝載到內存中。
     從而消耗很大的內存空間。而SAX是基於事件的。當某個事件被觸發時,才獲取
     相應的XML的部分數據,從而不管XML文件有多大,都只佔用了少量的內存空間。
     2. DOM可以讀取XML也可以向XML文件中插入數據,而SAX卻只能對XML進行讀取,
     而不能在文件中插入數據。這也是SAX的一個缺點。
     3.SAX的另一個缺點:DOM我們可以指定要訪問的元素進行隨機訪問,而SAX則不行。
     SAX是從文檔開始執行遍歷的。並且只能遍歷一次。也就是說我們不能隨機的訪問XML
     文件,只能從頭到尾的將XML文件遍歷一次(當然也可以中間截斷遍歷)。
*/
     
     
     
package com.softeem.xml.util;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.softeem.xml.dto.ClassDTO;
import com.softeem.xml.dto.CollegeDTO;
import com.softeem.xml.dto.StudentDTO;

public class SAXParseXML {
	
	public static void main(String[] args) throws Exception {
		String path = "D:/stus.xml";
		
		CollegeDTO college = saxParse(path);
		System.out.println(college.getClasses().get("計科1班").getStus().get("20111114").getStuName());
	}

	public static CollegeDTO saxParse(String path){
		CollegeDTO college = null;
		try {
			SAXReader reader = new SAXReader();
			//解析目標文件
			Document dom = reader.read(new File(path));
			//獲取根節點
			Element root = dom.getRootElement();
			//獲取根節點屬性
//		Attribute attr = root.attribute("id");
//		String attrValue = attr.getValue();
//		String cid = root.attribute("id").getValue();
			String cid = root.attributeValue("id");
			String cname = root.attributeValue("name");
			
			//獲取根節點的子節點
			List<Element> cses = root.elements("class");
			
			Map<String, ClassDTO> classes = new HashMap<String, ClassDTO>();
			for (int i = 0; i < cses.size(); i++) {
				Element cse = cses.get(i);
				String className = cse.attributeValue("className");
				String classNum = cse.attributeValue("classNum");
				List<Element> stes = cse.elements("student");
				
				Map<String, StudentDTO> stus = new HashMap<String, StudentDTO>();
				for (Element ste : stes) {
					String stuNum = ste.attributeValue("stuNum");
					String stuName = ste.attributeValue("stuName");
					String stuSex = ste.attributeValue("stuSex");
					String stuAge = ste.attributeValue("stuAge");
					StudentDTO stu = new StudentDTO(stuNum, stuName, stuSex, Integer.parseInt(stuAge));
					stus.put(stuNum, stu);
				}
				ClassDTO cla = new ClassDTO(className, Integer.parseInt(classNum), stus);
				classes.put(className, cla);
			}
			college = new CollegeDTO(Integer.parseInt(cid), cname, classes);
			
		} catch (Exception e) {
			System.err.println("出現錯誤!");
		}
		return college;
	}
}


<!--stus.xml文件-->
<?xml version="1.0" encoding="UTF-8"?>
<college id="1001" name="">
	<class className="計科1班" classNum="41">
		<student stuNum="20111111" stuName="" stuSex="男" stuAge="20" />
		<student stuNum="20111112" stuName="" stuSex="男" stuAge="21" />
		<student stuNum="20111113" stuName="" stuSex="女" stuAge="20" />
		<student stuNum="20111114" stuName="" stuSex="男" stuAge="19" />
	</class>
	<class className="網工1班" classNum="49">
		<student stuNum="20112111" stuName="" stuSex="男" stuAge="21" />
		<student stuNum="20112112" stuName="" stuSex="男" stuAge="20" />
		<student stuNum="20112113" stuName="" stuSex="女" stuAge="22" />
	</class>
</college>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章