• XML三种解析方式


    第一种dom解析:

    String xml = "<persons>" +
                        "<person>" +
                        "<name>amos</name>" +
                        "<age>30</age>" +
                        "</person>" +
                        "<person>" +
                        "<name>tom</name>" +
                        "<age>18</age>" +
                        "</person>" +
                        "</persons>";
    
                DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document parse = builder.parse(new ByteArrayInputStream(xml.getBytes()));
    
                Element element = parse.getDocumentElement();
    
                NodeList nodes = element.getElementsByTagName("person");
                for (int i = 0; i < nodes.getLength(); i++) {
                    NodeList person = nodes.item(i).getChildNodes();
                    for (int j = 0; j < person.getLength(); j++) {
                        if (person.item(j) instanceof Element)
                            Log.d("TAG","name:" + person.item(j).getNodeName() + ",value:" + person.item(j).getTextContent());
                    }
                }

    第二种sax解析:

     SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
     saxParser.parse(new ByteArrayInputStream(xml.getBytes()), new MyDefaultHandler());
    
    class MyDefaultHandler extends DefaultHandler {
    
            String currentTag;
    
            @Override
            public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
                super.startElement(uri, localName, qName, attributes);
                currentTag = localName;
            }
    
            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                super.characters(ch, start, length);
                String value = new String(ch, start, length); // 将当前TextNode转换为String
                Log.d("TAG","name:" + currentTag + ",value:" + value);
            }
        }

    第三种pull解析:

        XmlPullParser xmlPullParser = XmlPullParserFactory.newInstance().newPullParser();
                xmlPullParser.setInput(new ByteArrayInputStream(xml.getBytes()), "UTF-8");
                int eventType = xmlPullParser.getEventType();
                boolean isEnd = false;
                while (!isEnd) {
                    switch (eventType) {
                        case XmlPullParser.START_DOCUMENT:
                            Log.d("TAG","START_DOCUMENT---name:" + xmlPullParser.getName());
                            break;
                        case XmlPullParser.START_TAG:
                            if (xmlPullParser.getName().equals("name")) {
                                String name = xmlPullParser.nextText();
                                Log.d("TAG","name:" + xmlPullParser.getName() + ",value:" + name);
                            } else if (xmlPullParser.getName().equals("age")) {
                                String name = xmlPullParser.nextText();
                                Log.d("TAG","name:" + xmlPullParser.getName() + ",value:" + name);
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            Log.d("TAG","END_TAG---name:" + xmlPullParser.getName());
                            break;
                        case XmlPullParser.END_DOCUMENT:
                            Log.d("TAG","END_DOCUMENT---name:" + xmlPullParser.getName());
                            isEnd = true;
                            break;
                    }
                    eventType = xmlPullParser.next();
                }
  • 相关阅读:
    感知器算法--python实现
    PHP appserv + ZendStudio12.5.1 + 注册码
    第九周-每周例行报告
    第八周-每周例行报告
    感谢THUNDER
    第七周-每周例行报告
    第六周-每周例行报告
    第五周-每周例行报告
    第四周-四则运算试题生成
    第四周-单元测试
  • 原文地址:https://www.cnblogs.com/BruceV/p/12038476.html
Copyright © 2020-2023  润新知