DOM解析XML文檔


package com.softeem.xml.util;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import com.softeem.xml.dto.ClassDTO;
import com.softeem.xml.dto.CollegeDTO;
import com.softeem.xml.dto.StudentDTO;

public class DOMParseXML {
	
	public static void main(String[] args) throws Exception {
		
		String path = "D:/stus.xml";
		CollegeDTO c = parseXML(path);
		//獲取信息打印
		System.out.println(c.getClasses().get("計科1班").getStus().get("20111114").getStuName());
	}

	public static CollegeDTO parseXML(String path){
		CollegeDTO college = null;
		try {
			//dom解析
			
			//①加載被解析的xml文件
			//通過工廠類創建DocumentBuilder類的對象
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			
			DocumentBuilder builder = factory.newDocumentBuilder();
			//調用DocumentBuilder類中的parse方法,加載一個xml文件
			//dom對象就相當於一個xml文件
			Document dom = builder.parse(new File(path));
			
			//②將dom對象中加載的xml文件的數據,有順序的取出來
			//Document類中的getDocumentElement()方法:獲取到xml文件的根標籤
			Element root = dom.getDocumentElement();
			//1.獲取元素屬性
			//Element類中的getAttribute方法:從當前element對象元素中,根據屬性名取到屬性值
			//System.out.println(root.getTagName());
			
			//NodeList ns = root.getElementsByTagName("class");***********
			String cid = root.getAttribute("id");
			String cname = root.getAttribute("name");
			
			//2.獲取元素的子標籤
			NodeList nodes = root.getChildNodes();
			//System.out.println(nodes.getLength());
			
			Map<String, ClassDTO> classes = new HashMap<String, ClassDTO>();
			
			for (int i = 0; i < nodes.getLength(); i++) {
				Node node = nodes.item(i);
				String nodeName = node.getNodeName();
				//String nodeValue = node.getNodeValue();
				//int nodeType = node.getNodeType();//返回short型變量
				if("class".equals(nodeName)){
//					NamedNodeMap map = node.getAttributes();
//					Node attr = map.getNamedItem("className");
//					String className = attr.getNodeValue();
					String className = node.getAttributes().getNamedItem("className").getNodeValue();
					String cnum = node.getAttributes().getNamedItem("classNum").getNodeValue();
					
					//獲取節點的子節點
					NodeList ns2 = node.getChildNodes();
					
					Map<String, StudentDTO> stus = new HashMap<String, StudentDTO>();
					for (int j = 0; j < ns2.getLength(); j++) {
						Node n = ns2.item(j);
						if("student".equals(n.getNodeName())){
							String snum = n.getAttributes().getNamedItem("stuNum").getNodeValue();
							String sname = n.getAttributes().getNamedItem("stuName").getNodeValue();
							String ssex = n.getAttributes().getNamedItem("stuSex").getNodeValue();
							String sage = n.getAttributes().getNamedItem("stuAge").getNodeValue();
							//放進dto
							StudentDTO stu = new StudentDTO(snum, sname, ssex, Integer.parseInt(sage));
							stus.put(snum, stu);//放一個stu
						}
					}
					
					ClassDTO cla = new ClassDTO(className, Integer.parseInt(cnum), 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>



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