dom解析方法是将整个xml文档装载到内存当中,然后通过树形结构方式去解析的,这种方式只适合于在pc端的开发,不是很适合手机端的开发,毕竟来说手机的内存是没法跟pc相提并论的。
具体实现步骤如下:
第一步:准备好解析的xml文档
<?xml version="1.0" encoding="UTF-8"?> <students> <student id="A001"> <name>zhangsan</name> <sex>male</sex> <age>12</age> <address>beijing</address> </student> <student id="A002"> <name>lisi</name> <sex>male</sex> <age>13</age> <address>beijing</address> </student> </students>
第二步:定义一个实体类(这个实体类其实就是xml文档中的内容了)
package com.bjhksj.dom; public class Student { private String id; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } private String name; private String sex; private int age; private String address; }
第三步:下载xml文档
package com.bjhksj.dom; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class HttpDownload { public static InputStream downloader(String path) { InputStream inputStream = null; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); inputStream = conn.getInputStream(); } catch (Exception e) { // TODO: handle exception } return inputStream; } }
第四部:解析过程
package com.bjhksj.dom; import java.io.IOException; import java.io.InputStream; 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 DomXMLParser { public static List<Student> domXMLParser(InputStream inputStream) { // 创建一个Document解析器工厂对象 List<Student> studnetList = new ArrayList<Student>(); try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); Document document = documentBuilder.parse(inputStream); NodeList studentNodes = document.getElementsByTagName("student"); for (int i = 0; i < studentNodes.getLength(); i++) { Element element = (Element) studentNodes.item(i); Student student = new Student(); student.setId(element.getAttribute("id")); NodeList childNodes = element.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { if ("name".equals(childNodes.item(j).getNodeName())) { student.setName(childNodes.item(j).getFirstChild() .getNodeValue()); } else if ("sex".equals(childNodes.item(j).getNodeName())) { student.setSex(childNodes.item(j).getFirstChild() .getNodeValue()); } else if ("age".equals(childNodes.item(j).getNodeName())) { student.setAge(Integer.parseInt(childNodes.item(j) .getFirstChild().getNodeValue())); } else if ("address".equals(childNodes.item(j) .getNodeName())) { student.setAddress(childNodes.item(j).getFirstChild() .getNodeValue()); } } studnetList.add(student); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return studnetList; } }