本文由上海it培训网站提供,转载请注明。
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解析,更多精彩请移步至 上海java培训官网
//①加载被解析的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>