Android中的XML解析-DOM的使用与开发技巧
<?xml version="1.0" encoding="UTF-8"?><persons> <person id="1"> <name>叶问</name> <age>23</age> </person> <person id="2"> <name>李小龙</name> <age>17</age> </person></persons>
?
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.Node;import org.w3c.dom.NodeList;/** * 使用Dom解析xml文件 * */public class DomXMLReader {public static List<Person> readXML(InputStream inStream) { List<Person> persons = new ArrayList<Person>(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document dom = builder.parse(inStream); Element root = dom.getDocumentElement(); NodeList items = root.getElementsByTagName("person"); //查找所有person节点 for (int i = 0; i < items.getLength(); i++) { Person person = new Person(); //得到第一个person节点 Element personNode = (Element) items.item(i); //获取person节点的id属性值 person.setId(new Integer(personNode.getAttribute("id"))); //获取person节点下的所有子节点(标签之间的空白节点和name/age元素) NodeList childsNodes = personNode.getChildNodes(); for (int j = 0; j < childsNodes.getLength(); j++) { Node node = (Node) childsNodes.item(j); //判断是否为元素类型 if(node.getNodeType() == Node.ELEMENT_NODE){ Element childNode = (Element) node; //判断是否name元素 if ("name".equals(childNode.getNodeName())) { //获取name元素下Text节点,然后从Text节点获取数据 person.setName(childNode.getFirstChild().getNodeValue()); } else if (“age”.equals(childNode.getNodeName())) { person.setAge(new Short(childNode.getFirstChild().getNodeValue())); } } } persons.add(person); } inStream.close(); } catch (Exception e) { e.printStackTrace(); } return persons;}
public static String getValByTagName(Document doc, String tagName) { NodeList list = doc.getElementsByTagName(tagName); if (list.getLength() > 0) { Node node = list.item(0); Node valNode = node.getFirstChild(); if (valNode != null) { String val = valNode.getNodeValue(); return val; } } return null;}
?
public class Person(){ private String name; private String age; public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(String age){ this.age = age; } public String getAge(){ return age; }}
NodeList nodeList = doc.getElementsByTagName("person");int len = nodeList.getLength();for (int i = 0; i < len; i++) { Node node = nodeList.item(i); Person bean = (Person) convertToBean(Person.class, node);}public static Object convertToBean(Class clazz, Node ele) { Object object = null; try { object = clazz.newInstance(); Class[] parameterTypes = new Class[] { String.class }; NodeList list = ele.getChildNodes(); int len = list.getLength(); for (int i = 0; i < len; i++) { try { Node node = list.item(i); String nodeName = node.getNodeName(); String nodeVal = ""; Node valueNode = node.getFirstChild(); if (valueNode != null) { nodeVal = valueNode.getNodeValue(); } Object[] arguments = new Object[] { nodeVal }; if (nodeName.length() > 0) { String first = nodeName.substring(0, 1); String last = nodeName.substring(1); Method method = clazz.getMethod("set" + first.toUpperCase() + last, parameterTypes); method.invoke(object, arguments); } } catch (NoSuchMethodException e) { } } return object; } catch (Exception e) { e.printStackTrace(); return null; }}
?