《学习内容》:

    //========学习内容=========
    原生DOM方式解析XML文件
    JDOM和DOM4解析XML文件

    《知识点—笔记》
    //=========================

    《代码内容》:
    原生DOM解析XML文件

    1. package parsexml;
    2. import org.w3c.dom.Document;
    3. import org.w3c.dom.Element;
    4. import org.w3c.dom.NodeList;
    5. import org.xml.sax.SAXException;
    6. import javax.xml.parsers.DocumentBuilder;
    7. import javax.xml.parsers.DocumentBuilderFactory;
    8. import javax.xml.parsers.ParserConfigurationException;
    9. import java.io.File;
    10. import java.io.IOException;
    11. @SuppressWarnings("all")
    12. public class ParseXml {
    13. public static void main(String[] args) {
    14. try {
    15. //创建一个工厂 用来创建工人
    16. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    17. //用工厂创建工人
    18. DocumentBuilder builder = factory.newDocumentBuilder();
    19. //给工人图纸 让他去读取xml文件然后调用方法拿到dom
    20. Document document = builder.parse(new File("src//testxml//test.xml"));
    21. //通过dom去获取和遍历整个xml文件
    22. //获取根标记
    23. Element school = document.getDocumentElement();
    24. String schoolID = school.getAttribute("id");
    25. String schoolName = school.getAttribute("name");
    26. String loc = school.getAttribute("loc");
    27. System.out.println(schoolID+"--"+schoolName+"--"+loc);
    28. //获取学校下面的班级
    29. NodeList classEleList = document.getElementsByTagName("class");
    30. for (int i = 0; i < classEleList.getLength(); i++) {
    31. //获取每个班级的属性
    32. Element classElem = (Element)classEleList.item(i);
    33. String classID = classElem.getAttribute("id");
    34. String className = classElem.getAttribute("name");
    35. String classLOC = classElem.getAttribute("loc");
    36. System.out.println("\t"+classID+"--"+className+"--"+classLOC);
    37. //获取班级下的 老师标签 属性
    38. NodeList teacherList = classElem.getElementsByTagName("teacher");
    39. for (int j = 0; j < teacherList.getLength(); j++) {
    40. Element teacherEle = (Element)teacherList.item(j);
    41. String teacherID = teacherEle.getAttribute("id");
    42. String teacherName = teacherEle.getAttribute("name");
    43. String sex = teacherEle.getElementsByTagName("sex").item(0).getTextContent();
    44. System.out.println("\t \t"+teacherID+"--"+teacherName+"--"+sex);
    45. }
    46. //获取班级下 学生标签 属性
    47. NodeList studentList = classElem.getElementsByTagName("student");
    48. for (int j = 0; j < studentList.getLength(); j++) {
    49. Element studentEle = (Element)studentList.item(j);
    50. String studentID = studentEle.getAttribute("id");
    51. String studentName = studentEle.getAttribute("name");
    52. String sex = studentEle.getElementsByTagName("sex").item(0).getTextContent();
    53. System.out.println("\t \t"+studentID+"--"+studentName+"--"+sex);
    54. }
    55. }
    56. } catch (ParserConfigurationException e) {
    57. e.printStackTrace();
    58. } catch (SAXException e) {
    59. e.printStackTrace();
    60. } catch (IOException e) {
    61. e.printStackTrace();
    62. }
    63. }
    64. }

    JDOM解析XML文件

    1. package parsexml;
    2. import org.jdom2.Document;
    3. import org.jdom2.Element;
    4. import org.jdom2.JDOMException;
    5. import org.jdom2.input.SAXBuilder;
    6. import java.io.File;
    7. import java.io.IOException;
    8. import java.util.List;
    9. public class JDom {
    10. public static void main(String[] args) {
    11. try {
    12. SAXBuilder saxBuilder = new SAXBuilder();
    13. Document document = saxBuilder.build(new File("src/testxml/test.xml"));
    14. //获取根标签
    15. Element school = document.getRootElement();
    16. //获取根标签的属性
    17. String schoolId = school.getAttributeValue("id");
    18. String schollName = school.getAttributeValue("name");
    19. String schoolLOC = school.getAttributeValue("loc");
    20. System.out.println(schoolId+"--"+schollName+"--"+schoolLOC);
    21. //通过school根标签去获取子标签
    22. List<Element> classList = school.getChildren("class");
    23. for (Element classEle : classList){
    24. String classID = classEle.getAttributeValue("id");
    25. String className = classEle.getAttributeValue("name");
    26. String clasLOC = classEle.getAttributeValue("loc");
    27. System.out.println("\t"+classID+"--"+className+"--"+clasLOC);
    28. //获取下面的老师和学生
    29. List<Element> teacher = classEle.getChildren("teacher");
    30. for (Element teacherEle : teacher){
    31. String teacherID = teacherEle.getAttributeValue("id");
    32. String teacherName = teacherEle.getAttributeValue("name");
    33. String sex = teacherEle.getChildText("sex");
    34. System.out.println("\t \t"+teacherID+"--"+teacherName+"--"+sex);
    35. }
    36. List<Element> studentList = classEle.getChildren("student");
    37. for (Element studentEle : studentList){
    38. String studentID = studentEle.getAttributeValue("id");
    39. String studentName = studentEle.getAttributeValue("name");
    40. String studentSex = studentEle.getChildText("sex");
    41. System.out.println("\t \t"+studentID+"--"+studentName+"--"+studentSex);
    42. }
    43. }
    44. } catch (JDOMException e) {
    45. e.printStackTrace();
    46. } catch (IOException e) {
    47. e.printStackTrace();
    48. }
    49. }
    50. }

    DOM4解析XML文**件**

    1. package parsexml;
    2. import org.dom4j.Document;
    3. import org.dom4j.DocumentException;
    4. import org.dom4j.Element;
    5. import org.dom4j.io.SAXReader;
    6. import java.io.File;
    7. import java.util.List;
    8. public class DOM4 {
    9. public static void main(String[] args) {
    10. try {
    11. SAXReader saxReader = new SAXReader();
    12. Document document = saxReader.read(new File("src/testxml/test.xml"));
    13. Element school = document.getRootElement();
    14. String schoolID = school.attributeValue("id");
    15. String schoolName = school.attributeValue("name");
    16. String schoolLOC = school.attributeValue("loc");
    17. System.out.println(schoolID+"--"+schoolName+"--"+schoolLOC);
    18. List<Element> classList = school.elements("class");
    19. for (Element classEle : classList){
    20. String classID = classEle.attributeValue("id");
    21. String className = classEle.attributeValue("name");
    22. String classLOC = classEle.attributeValue("loc");
    23. System.out.println("\t"+classID+"--"+className+"--"+classLOC);
    24. List<Element> teacherList = classEle.elements("teacher");
    25. for (Element teacherEle : teacherList){
    26. String teacherID = teacherEle.attributeValue("id");
    27. String teacherName = teacherEle.attributeValue("name");
    28. String teacherSex = teacherEle.elementText("sex");
    29. System.out.println("\t \t" + teacherID + "--"+ teacherName+"--"+teacherSex);
    30. }
    31. List<Element> studentList = classEle.elements("student");
    32. for (Element studentEle : studentList){
    33. String studentID = studentEle.attributeValue("id");
    34. String studentName = studentEle.attributeValue("name");
    35. String studentSex = studentEle.elementText("sex");
    36. System.out.println("\t \t"+studentID+"--"+studentName+"--"+studentSex);
    37. }
    38. }
    39. } catch (DocumentException e) {
    40. e.printStackTrace();
    41. }
    42. }
    43. }

    学习总结:
    学懂的:
    没有什么不懂的,这个就是简单的写流程,但是DOM4的效率更高,他是基于JDOM又再次封装的,而JDOM是基于原生的DOM和
    SEX封装的。
    有问题的地方:

    **