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=女