1.解析xml文档
/* * 解析xml文件 * xml文件实例如下: <?xml version="1.0" encoding="utf-8"?> <employees> <employee> <name>ddviplinux</name> <sex>m</sex> <age>30</age> </employee> <employee> <name>李路</name> <sex>女</sex> <age>20</age> </employee> </employees> * */ public void parseXml(String fileName) { try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.parse(fileName); NodeList employees = ((Node) document).getChildNodes(); //System.out.println(employees.getLength());//length = 1 代表标签<employees> for (int i = 0; i < employees.getLength(); i++) { Node employee = employees.item(i); NodeList employeeInfo = employee.getChildNodes(); //System.out.println(employeeInfo.getLength()); //length = 5 代表标签<employee> </employee> <employee> </employee> </employees> for (int j = 0; j < employeeInfo.getLength(); j++) { if(j%2 == 0) continue; //在执行到标签</employee>的时候才能读取<employee> </employee>中间的数据 Node node = employeeInfo.item(j); NodeList employeeMeta = node.getChildNodes(); //System.out.println(j+" "+employeeMeta.getLength()); //length = 7 代表标签 <name> </name> <sex> </sex> <age> </age> </employee> for (int k = 0; k < employeeMeta.getLength(); k++) { if(k%2 == 0) continue; //在执行到标签</name>的时候能读取<name> </name>中间的数据 下面的性别和年龄同上。 System.out.println(employeeMeta.item(k).getNodeName() + ":" + employeeMeta.item(k).getTextContent()); } } } System.out.println("解析完毕"); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } }
1.2根据数据流解析xml文件到HashMap
/* * xml文件示例 * <?xml version="1.0" encoding="utf-8"?> <update> <version>1.7</version> <name>CerClient</name> <url>http://gdown.baidu.com/data/wisegame/f98d235e39e29031/baiduxinwen.apk</url> </update> * */ public class ParseXmlService { public HashMap<String,String> parseXml(InputStream inStream) throws Exception { HashMap<String,String> hashMap = new HashMap<String,String>(); // 实例化一个文档构建器工厂 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // 通过文档构建器工厂获取一个文档构建器 DocumentBuilder builder = factory.newDocumentBuilder(); // 通过文档通过文档构建器构建一个文档实例 Document document = builder.parse(inStream); //获取XML文件根节点 Element root = document.getDocumentElement(); //获得所有子节点 NodeList childNodes = root.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { //遍历子节点 Node childNode = (Node) childNodes.item(j); if (childNode.getNodeType() == Node.ELEMENT_NODE) { Element childElement = (Element) childNode; //版本号 if ("version".equals(childElement.getNodeName())) { hashMap.put("version",childElement.getFirstChild().getNodeValue()); } //软件名称 else if (("name".equals(childElement.getNodeName()))) { hashMap.put("name",childElement.getFirstChild().getNodeValue()); } //下载地址 else if (("url".equals(childElement.getNodeName()))) { hashMap.put("url",childElement.getFirstChild().getNodeValue()); } } } return hashMap; } }
参考网址:
1.http://developer.51cto.com/art/200903/117512.htm
2.http://www.cnblogs.com/coolszy/archive/2012/04/27/2474279.html
原文链接:https://www.f2er.com/xml/297721.html