XML三种解析

这是一个xml文件

XML三种解析_第1张图片

使用Dom解析xml文件

 public void dom(){
        DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();//获得解析工厂
        try {
            DocumentBuilder builder=documentBuilderFactory.newDocumentBuilder();//创建builder对象
            InputStream inputStream = getAssets().open("book.xml");//获得XML文件
            bookBeans=new ArrayList<>();
            Document document=builder.parse(inputStream);//把整个文件添加到内存中(为什么需要把文件读到内存?因为内存比读SD卡快)

            NodeList nodeList=document.getElementsByTagName("book");//获取文件中所有节点名为book的内容,并打算将这些内容存入到list集合中

            //由于有好多个book节点,所以我们要对lis做遍历
            for(int i=0;i

Pull解析Xml文件

public void Pull_xml(){
        try {
            XmlPullParserFactory xmlPullParserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser xmlPullParser = xmlPullParserFactory.newPullParser();
            InputStream inputStream = getAssets().open("book.xml");
            xmlPullParser.setInput(inputStream,"utf-8");
            int eventType = xmlPullParser.getEventType();
            BookBean bookBean=null;
            while (eventType!=XmlPullParser.END_DOCUMENT){//文档开头
                switch (eventType){
                    case XmlPullParser.START_TAG:
                        if(xmlPullParser.getName().equals("book")){
                            bookBean=new BookBean();
                            String id = xmlPullParser.getAttributeValue(null, "id");
                            bookBean.id= Integer.parseInt(id);
                        }else if(xmlPullParser.getName().equals("name")){
                            String name = xmlPullParser.nextText();
                            bookBean.name=name;
                        }else if(xmlPullParser.getName().equals("author")){
                            String author = xmlPullParser.nextText();
                            bookBean.author=author;
                        }else if(xmlPullParser.getName().equals("year")){
                            String year = xmlPullParser.nextText();
                            bookBean.year=Integer.parseInt(year);
                        }else if(xmlPullParser.getName().equals("price")){
                            String price = xmlPullParser.nextText();
                            bookBean.prcie=Integer.parseInt(price);
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if(xmlPullParser.getName().equals("book")) {
                            bookBeans.add(bookBean);
                        }
                        break;

                }
                // 再此读取下一标签
                eventType=xmlPullParser.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Sax解析xml文件

自定义一个类继承DefaultHandler,然后重写5个方法,其中有4中情况

1.文档开始
2.文档结束
3.标签开始
4.标签结束

 class MyHandler extends DefaultHandler{


        BookBean bookBean;
        String Tem;//用来存放解出来的字符串
        //文档开始
        @Override
        public void startDocument() throws SAXException {
            super.startDocument();
            bookBeans=new ArrayList<>();

        }

        //文档结束
        @Override
        public void endDocument() throws SAXException {
            super.endDocument();
            //文档结束不做处理
        }

        //标签开始
        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
            super.startElement(uri, localName, qName, attributes);
            if(qName.equals("book")){
                bookBean=new BookBean();
                bookBean.id= Integer.parseInt(attributes.getValue("id"));
            }
        }
          //标签结束
        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(uri, localName, qName);
            if(qName.equals("name")){
                bookBean.name=Tem;
            }else if(qName.equals("author")){
                bookBean.author= Tem;
            }else if(qName.equals("year")){
                bookBean.year= Integer.parseInt(Tem);
            }else if(qName.equals("price")){
                bookBean.prcie= Integer.parseInt(Tem);
            }
            if(qName.equals("book")){
                bookBeans.add(bookBean);
            }
        }

        //类型转换
        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            super.characters(ch, start, length);

            Tem=new String(ch,start,length);
        }

    }

接下来使用我们自定义的类实现Sax解析

XML三种解析_第2张图片

你可能感兴趣的:(解析)