android三种操作XML的方法总结
更新时间:2023-11-08方法一:SAX解析(Simple API for XML)
SAX解析采用流模式,以事件为驱动,遇到开始节点、结束节点、元素内容等事件时都会触发对应的事件处理方法,适合处理大型XML文件。
try { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean bName = false; boolean bAge = false; boolean bGender = false; public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("name")) { bName = true; } else if (qName.equalsIgnoreCase("age")) { bAge = true; } else if (qName.equalsIgnoreCase("gender")) { bGender = true; } } public void characters(char ch[], int start, int length) throws SAXException { if (bName) { System.out.println("Name : " + new String(ch, start, length)); bName = false; } else if (bAge) { System.out.println("Age : " + new String(ch, start, length)); bAge = false; } else if (bGender) { System.out.println("Gender : " + new String(ch, start, length)); bGender = false; } } }; saxParser.parse("file.xml", handler); } catch (Exception e) { e.printStackTrace(); }
方法二:DOM解析(Document Object Model)
DOM解析是将整个XML文件加载到内存中,形成DOM树,通过对DOM树的操作实现对XML的解析,可对XML进行增删查改等操作,但不适合处理大型XML文件。
try { File inputFile = new File("file.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); NodeList nodeList = doc.getElementsByTagName("user"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; System.out.println("Name : "+element.getElementsByTagName("name").item(0).getTextContent()); System.out.println("Age : "+element.getElementsByTagName("age").item(0).getTextContent()); System.out.println("Gender : "+element.getElementsByTagName("gender").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); }
方法三:Pull解析
Pull解析是一种基于事件驱动的解析方式,也适合处理大型XML文件。与SAX解析类似,但是对解析过程中的事件进行了优化,使得解析效率更高。
try { XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance(); XmlPullParser parser = xmlPullParserFactory.newPullParser(); FileInputStream fis = new FileInputStream("file.xml"); parser.setInput(fis, "UTF-8"); int eventType = parser.getEventType(); String name = ""; String age = ""; String gender = ""; while (eventType != XmlPullParser.END_DOCUMENT) { String nodeName = parser.getName(); switch (eventType) { case XmlPullParser.START_TAG: if ("name".equals(nodeName)) { name = parser.nextText(); } else if ("age".equals(nodeName)) { age = parser.nextText(); } else if ("gender".equals(nodeName)) { gender = parser.nextText(); } break; case XmlPullParser.END_TAG: if ("user".equals(nodeName)) { System.out.println("Name : "+name); System.out.println("Age : "+age); System.out.println("Gender : "+gender); } break; default: break; } eventType = parser.next(); } } catch (Exception e) { e.printStackTrace(); }
总结:
对于XML的解析,这三种方式都能够实现。SAX解析适合处理大型XML文件,但没有DOM解析和Pull解析方便;DOM解析可以实现对XML的增删查改等操作,但不适合处理大型XML文件;Pull解析也适合处理大型XML文件,但是需要手动触发下一个事件,对程序员的能力要求较高。因此,在选择XML解析方式时需要根据实际情况进行考虑,不能一概而论。