Android 开发工具类 20_DOMPersonService
xml 文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <persons> 3 <person id = "23"> 4 <name>NEwii</name> 5 <age>21</age> 6 </person> 7 <person id = "20"> 8 <name>WiiWill</name> 9 <age>25</age> 10 </person> 11 </persons>
DOM 格式
DOM 解析 XML
1 import java.io.InputStream; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 import javax.xml.parsers.DocumentBuilder; 6 import javax.xml.parsers.DocumentBuilderFactory; 7 8 import org.w3c.dom.Document; 9 import org.w3c.dom.Element; 10 import org.w3c.dom.Node; 11 import org.w3c.dom.NodeList; 12 13 import com.wangjialin.domain.Person; 14 15 public class DOMPersonService { 16 17 public static List<Person> getPersons(InputStream inStream) throws Exception{ 18 19 List<Person> persons = new ArrayList<Person>(); 20 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 21 DocumentBuilder builder = factory.newDocumentBuilder(); 22 Document document = builder.parse(inStream); 23 Element root = document.getDocumentElement(); 24 NodeList personNodes = root.getElementsByTagName("person"); 25 26 for(int i=0; i < personNodes.getLength() ; i++){ 27 28 Element personElement = (Element)personNodes.item(i); 29 int id = new Integer(personElement.getAttribute("id")); 30 Person person = new Person(); 31 person.setId(id); 32 NodeList childNodes = personElement.getChildNodes(); 33 34 for(int y=0; y < childNodes.getLength(); y++){ 35 if(childNodes.item(y).getNodeType() == Node.ELEMENT_NODE){ 36 if("name".equals(childNodes.item(y).getNodeName())){ 37 String name = childNodes.item(y).getFirstChild().getNodeValue(); 38 person.setName(name); 39 }else if("age".equals(childNodes.item(y).getNodeName())){ 40 String age = childNodes.item(y).getFirstChild().getNodeValue(); 41 person.setAge(new Short(age)); 42 } 43 } 44 } 45 persons.add(person); 46 } 47 inStream.close(); 48 return persons; 49 } 50 }
Person.java
1 public class Person { 2 3 private Integer id; 4 private String name; 5 private Short age; 6 7 public Person(){} 8 9 public Person(Integer id, String name, Short age) { 10 this.id = id; 11 this.name = name; 12 this.age = age; 13 } 14 15 public Person(String name, Short age) { 16 this.name = name; 17 this.age = age; 18 } 19 20 public Integer getId() { 21 return id; 22 } 23 24 public void setId(Integer id) { 25 this.id = id; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public Short getAge() { 37 return age; 38 } 39 40 public void setAge(Short age) { 41 this.age = age; 42 } 43 44 @Override 45 public String toString() { 46 return "id=" + id + ",name=" + name+ ",age=" + age; 47 } 48 49 50 }
JUnit 测试
1 public void testDOMgetPersons() throws Throwable{ 2 3 InputStream inStream = this.getClass().getClassLoader().getResourceAsStream("wangjialin.xml"); 4 List<Person> persons = DOMPersonService.getPersons(inStream); 5 6 for(Person person : persons){ 7 Log.i(TAG, person.toString()); 8 } 9 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。