《学习内容》:
//========学习内容=========
原生DOM方式解析XML文件
JDOM和DOM4解析XML文件
《知识点—笔记》
//=========================
《代码内容》:
原生DOM解析XML文件
package parsexml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
@SuppressWarnings("all")
public class ParseXml {
public static void main(String[] args) {
try {
//创建一个工厂 用来创建工人
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//用工厂创建工人
DocumentBuilder builder = factory.newDocumentBuilder();
//给工人图纸 让他去读取xml文件然后调用方法拿到dom
Document document = builder.parse(new File("src//testxml//test.xml"));
//通过dom去获取和遍历整个xml文件
//获取根标记
Element school = document.getDocumentElement();
String schoolID = school.getAttribute("id");
String schoolName = school.getAttribute("name");
String loc = school.getAttribute("loc");
System.out.println(schoolID+"--"+schoolName+"--"+loc);
//获取学校下面的班级
NodeList classEleList = document.getElementsByTagName("class");
for (int i = 0; i < classEleList.getLength(); i++) {
//获取每个班级的属性
Element classElem = (Element)classEleList.item(i);
String classID = classElem.getAttribute("id");
String className = classElem.getAttribute("name");
String classLOC = classElem.getAttribute("loc");
System.out.println("\t"+classID+"--"+className+"--"+classLOC);
//获取班级下的 老师标签 属性
NodeList teacherList = classElem.getElementsByTagName("teacher");
for (int j = 0; j < teacherList.getLength(); j++) {
Element teacherEle = (Element)teacherList.item(j);
String teacherID = teacherEle.getAttribute("id");
String teacherName = teacherEle.getAttribute("name");
String sex = teacherEle.getElementsByTagName("sex").item(0).getTextContent();
System.out.println("\t \t"+teacherID+"--"+teacherName+"--"+sex);
}
//获取班级下 学生标签 属性
NodeList studentList = classElem.getElementsByTagName("student");
for (int j = 0; j < studentList.getLength(); j++) {
Element studentEle = (Element)studentList.item(j);
String studentID = studentEle.getAttribute("id");
String studentName = studentEle.getAttribute("name");
String sex = studentEle.getElementsByTagName("sex").item(0).getTextContent();
System.out.println("\t \t"+studentID+"--"+studentName+"--"+sex);
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JDOM解析XML文件
package parsexml;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class JDom {
public static void main(String[] args) {
try {
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(new File("src/testxml/test.xml"));
//获取根标签
Element school = document.getRootElement();
//获取根标签的属性
String schoolId = school.getAttributeValue("id");
String schollName = school.getAttributeValue("name");
String schoolLOC = school.getAttributeValue("loc");
System.out.println(schoolId+"--"+schollName+"--"+schoolLOC);
//通过school根标签去获取子标签
List<Element> classList = school.getChildren("class");
for (Element classEle : classList){
String classID = classEle.getAttributeValue("id");
String className = classEle.getAttributeValue("name");
String clasLOC = classEle.getAttributeValue("loc");
System.out.println("\t"+classID+"--"+className+"--"+clasLOC);
//获取下面的老师和学生
List<Element> teacher = classEle.getChildren("teacher");
for (Element teacherEle : teacher){
String teacherID = teacherEle.getAttributeValue("id");
String teacherName = teacherEle.getAttributeValue("name");
String sex = teacherEle.getChildText("sex");
System.out.println("\t \t"+teacherID+"--"+teacherName+"--"+sex);
}
List<Element> studentList = classEle.getChildren("student");
for (Element studentEle : studentList){
String studentID = studentEle.getAttributeValue("id");
String studentName = studentEle.getAttributeValue("name");
String studentSex = studentEle.getChildText("sex");
System.out.println("\t \t"+studentID+"--"+studentName+"--"+studentSex);
}
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DOM4解析XML文**件**
package parsexml;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;
public class DOM4 {
public static void main(String[] args) {
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(new File("src/testxml/test.xml"));
Element school = document.getRootElement();
String schoolID = school.attributeValue("id");
String schoolName = school.attributeValue("name");
String schoolLOC = school.attributeValue("loc");
System.out.println(schoolID+"--"+schoolName+"--"+schoolLOC);
List<Element> classList = school.elements("class");
for (Element classEle : classList){
String classID = classEle.attributeValue("id");
String className = classEle.attributeValue("name");
String classLOC = classEle.attributeValue("loc");
System.out.println("\t"+classID+"--"+className+"--"+classLOC);
List<Element> teacherList = classEle.elements("teacher");
for (Element teacherEle : teacherList){
String teacherID = teacherEle.attributeValue("id");
String teacherName = teacherEle.attributeValue("name");
String teacherSex = teacherEle.elementText("sex");
System.out.println("\t \t" + teacherID + "--"+ teacherName+"--"+teacherSex);
}
List<Element> studentList = classEle.elements("student");
for (Element studentEle : studentList){
String studentID = studentEle.attributeValue("id");
String studentName = studentEle.attributeValue("name");
String studentSex = studentEle.elementText("sex");
System.out.println("\t \t"+studentID+"--"+studentName+"--"+studentSex);
}
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
学习总结:
学懂的:
没有什么不懂的,这个就是简单的写流程,但是DOM4的效率更高,他是基于JDOM又再次封装的,而JDOM是基于原生的DOM和
SEX封装的。
有问题的地方:
无
**