1、About documentBuilderFactory API description
1)、 javax.xml.parsers 包DocumentBuilderFactory创建DOM模式的解析器对象, DocumentBuilderFactory是抽象工厂类,不能直接实例化,但是有newInstance方法
2)、DocumentBuilderFactory.newInstance() 得到创建 DOM 解析器的工厂
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance()
3)、newDocumentBuilder方法得到 DOM 解析器对象
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
4)、DOM解析器解析输入流,这里可以是XML,文档转化为输入流,或者字符串转为ByteArrayInputStream,DOM 解析器对象的 parse() 方法解析 XML 文档,得到代表整个文档的 Document 对象
- InputStream is = new FileInputStream("xxx.xml");
- documentBuilder.parse(is);
- documentBuilder.parse(new ByteArrayInputStream(str.getBytes()));
5)、得到 XML 文档的根节点
Element rootElement = document.getDocumentElement();
6)、得到节点的子节点
NodeList studentInfo = appElement.getChildNodes();
2、Test Demo
Student.java文件
- package com.parse.doc;
-
- public class Student {
-
- public String name;
- public String age;
- public String sex;
- public Student() {}
-
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAge() {
- return age;
- }
- public void setAge(String age) {
- this.age = age;
- }
- public String getSex() {
- return sex;
- }
- public void setSex(String sex) {
- this.sex = sex;
- }
- @Override
- public String toString() {
- return "name=" + name + ", age=" + age + ", sex=" + sex;
- }
- }
ParseXml.java文件
- package com.parse.doc;
-
- import java.io.ByteArrayInputStream;
- import java.util.ArrayList;
- import java.util.List;
-
- import javax.xml.parsers.DocumentBuilder;
- import javax.xml.parsers.DocumentBuilderFactory;
-
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
-
-
- public class ParseXml {
-
- public static String xml = "<Students><student><name><![CDATA[陈喻]]></name><age><![CDATA[26]]></age><sex><![CDATA[男]]></sex></student><student><name><![CDATA[陈彩凤]]></name><age><![CDATA[25]]></age><sex><![CDATA[女]]></sex></student><student><name><![CDATA[陈紫宣]]></name><age><![CDATA[2]]></age><sex><![CDATA[女]]></sex></student><student><name><![CDATA[陈紫曦]]></name><age><![CDATA[7个月]]></age><sex><![CDATA[女]]></sex></student></Students>";
- public static final String STUDENT = "student";
- public static final String NAME = "name";
- public static final String AGE = "age";
- public static final String SEX = "sex";
-
- public static void main(String args[]) {
- List<Student> students = parseXmlByDoc(xml);
- if (students != null && students.size() > 0) {
- for (Student student : students) {
- System.out.println(student);
- }
- } else {
- System.out.println("students size is 0");
- }
-
- }
-
- public static List<Student> parseXmlByDoc(String str) {
- if (str == null || "".equals(str)) {
- System.out.println("str is null or ''");
- return null;
- }
- DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
- List<Student> students = new ArrayList<Student>();
- DocumentBuilder documentBuilder;
- try {
- documentBuilder = documentBuilderFactory.newDocumentBuilder();
- Document document = documentBuilder.parse(new ByteArrayInputStream(str.getBytes()));
- Element rootElement = document.getDocumentElement();
- NodeList studentNodeList = rootElement.getElementsByTagName(STUDENT);
- for (int i = 0; i < studentNodeList.getLength(); i++) {
- Element appElement = (Element) studentNodeList.item(i);
- NodeList studentInfo = appElement.getChildNodes();
- Student student = new Student();
- for(int j = 0; j < studentInfo.getLength(); j++) {
- Element element = (Element) studentInfo.item(j);
- String appAttr = element.getTagName();
- switch (appAttr) {
- case NAME:
- student.setName(element.getTextContent());
- break;
- case AGE:
- student.setAge(element.getTextContent());
- break;
- case SEX:
- student.setSex(element.getTextContent());
- break;
- default:
- break;
- }
- }
- students.add(student);
- }
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- return students;
- }
- }
3、Running results
- name=陈喻, age=26, sex=男
- name=陈彩凤, age=25, sex=女
- name=陈紫宣, age=2, sex=女
- name=陈紫曦, age=7个月, sex=女