关键词搜索

源码搜索 ×
×

java之DocumentBuilderFactory解析xml

发布2018-06-01浏览14352次

详情内容

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 对象

 

 

  1. InputStream is = new FileInputStream("xxx.xml");
  2. documentBuilder.parse(is);
  3. documentBuilder.parse(new ByteArrayInputStream(str.getBytes()));


 
5)、得到 XML 文档的根节点

 

Element rootElement = document.getDocumentElement();



6)、得到节点的子节点

 

 

 

NodeList studentInfo = appElement.getChildNodes();

 

 

 

 

2、Test Demo

 

 

 

 

Student.java文件

  1. package com.parse.doc;
  2. public class Student {
  3. public String name;
  4. public String age;
  5. public String sex;
  6. public Student() {}
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public String getAge() {
  14. return age;
  15. }
  16. public void setAge(String age) {
  17. this.age = age;
  18. }
  19. public String getSex() {
  20. return sex;
  21. }
  22. public void setSex(String sex) {
  23. this.sex = sex;
  24. }
  25. @Override
  26. public String toString() {
  27. return "name=" + name + ", age=" + age + ", sex=" + sex;
  28. }
  29. }

ParseXml.java文件

  1. package com.parse.doc;
  2. import java.io.ByteArrayInputStream;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import javax.xml.parsers.DocumentBuilder;
  6. import javax.xml.parsers.DocumentBuilderFactory;
  7. import org.w3c.dom.Document;
  8. import org.w3c.dom.Element;
  9. import org.w3c.dom.NodeList;
  10. public class ParseXml {
  11. 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>";
  12. public static final String STUDENT = "student";
  13. public static final String NAME = "name";
  14. public static final String AGE = "age";
  15. public static final String SEX = "sex";
  16. public static void main(String args[]) {
  17. List<Student> students = parseXmlByDoc(xml);
  18. if (students != null && students.size() > 0) {
  19. for (Student student : students) {
  20. System.out.println(student);
  21. }
  22. } else {
  23. System.out.println("students size is 0");
  24. }
  25. }
  26. public static List<Student> parseXmlByDoc(String str) {
  27. if (str == null || "".equals(str)) {
  28. System.out.println("str is null or ''");
  29. return null;
  30. }
  31. DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  32. List<Student> students = new ArrayList<Student>();
  33. DocumentBuilder documentBuilder;
  34. try {
  35. documentBuilder = documentBuilderFactory.newDocumentBuilder();
  36. Document document = documentBuilder.parse(new ByteArrayInputStream(str.getBytes()));
  37. Element rootElement = document.getDocumentElement();
  38. NodeList studentNodeList = rootElement.getElementsByTagName(STUDENT);
  39. for (int i = 0; i < studentNodeList.getLength(); i++) {
  40. Element appElement = (Element) studentNodeList.item(i);
  41. NodeList studentInfo = appElement.getChildNodes();
  42. Student student = new Student();
  43. for(int j = 0; j < studentInfo.getLength(); j++) {
  44. Element element = (Element) studentInfo.item(j);
  45. String appAttr = element.getTagName();
  46. switch (appAttr) {
  47. case NAME:
  48. student.setName(element.getTextContent());
  49. break;
  50. case AGE:
  51. student.setAge(element.getTextContent());
  52. break;
  53. case SEX:
  54. student.setSex(element.getTextContent());
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. students.add(student);
  61. }
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. return null;
  65. }
  66. return students;
  67. }
  68. }

 

 

 

 

3、Running results

 

  1. name=陈喻, age=26, sex=男
  2. name=陈彩凤, age=25, sex=女
  3. name=陈紫宣, age=2, sex=女
  4. name=陈紫曦, age=7个月, sex=女

 

 

 

 

 

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载