SAX Parser:如何只读取`Item`标签?

时间:2018-05-22 07:52:12

标签: java android xml saxparser

我正在使用SAX Parser来解析一些XML内容。请检查下面的代码。

 public void parse(InputSource is, AppDataBean appDataBean) throws RuntimeException {

        int limitCheck;

        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            Log.d("SAX",appDataBean.getUrl());


            DefaultHandler handler = new DefaultHandler() {

                boolean title = false;
                boolean link = false;
                boolean author = false;

                public void startElement(String uri, String localName,
                                         String qName, Attributes attributes)
                        throws SAXException {

                    if (qName.equalsIgnoreCase(TITLE)) {
                        title = true;
                    }

                    if (qName.equalsIgnoreCase(LINK)) {
                        link = true;
                    }

                    if (qName.equalsIgnoreCase(AUTHOR)) {
                        author = true;
                    }


                    //Log.d("SAX","Start Element :" + qName);



                }

                public void endElement(String uri, String localName,
                                       String qName)
                        throws SAXException {




                }

                public void characters(char ch[], int start, int length)
                        throws SAXException {

                    System.out.println(new String(ch, start, length));


                    if (title) {
                        Log.d("SAX","End Element :" + "First Name : "
                                + new String(ch, start, length));

                        title = false;
                    }

                    if (link) {


                        Log.d("SAX","End Element :" + "Last Name : "
                                + new String(ch, start, length));
                        link = false;
                    }

                    if (author) {

                        Log.d("SAX","End Element :" + "Nick Name : "
                                + new String(ch, start, length));

                        author = false;
                    }

                }

            };

            saxParser.parse(is, handler);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

以下是我的XML的样子。

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
   <?xml-stylesheet type="text/xsl" href="rss.xsl"?>
   <channel>
      <title>MyRSS</title>
      <atom:link href="http://www.example.com/rss.php" rel="self" type="application/rss+xml" />
      <link>http://www.example.com/rss.php</link>
      <description>MyRSS</description>
      <language>en-us</language>
      <pubDate>Tue, 22 May 2018 13:15:15 +0530</pubDate>
      <item>
         <title>Title 1</title>
         <pubDate>Tue, 22 May 2018 13:14:40 +0530</pubDate>
         <link>http://www.example.com/news.php?nid=47610</link>
         <guid>http://www.example.com/news.php?nid=47610</guid>
         <description>bla bla bla</description>
      </item>
</channel>
</rss>

但是在这里,我要避免使用Channel标记,只读取根标记为Item。然后我才能得到真实的内容。我怎么能这样做?

更新

正如答案所示,我尝试将SAX Parser与stack一起使用。下面是代码,但我仍然不好,现在它没有为First Name

打印任何内容
public void parse(InputSource is,  AppDataBean appDataBean) throws RuntimeException {

        int limitCheck;
        stack = new Stack<>();


        try {

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();

            Log.d("SAX", appDataBean.getUrl());


            DefaultHandler handler = new DefaultHandler() {

                boolean title = false;
                boolean link = false;
                boolean author = false;

                public void startElement(String uri, String localName,
                                         String qName, Attributes attributes)
                        throws SAXException {

                    Log.d("SAX", "localName: " + localName);

                    if(localName.equalsIgnoreCase("item"))
                    {
                        stack = new Stack<>();
                        stack.push(qName);
                    }


                    if (qName.equalsIgnoreCase(TITLE)) {
                        if(stack.peek().equalsIgnoreCase("item"))
                        {
                            title = true;
                        }

                    }

                    if (qName.equalsIgnoreCase(LINK)) {
                        link = true;
                    }

                    if (qName.equalsIgnoreCase(AUTHOR)) {
                        author = true;
                    }


                    //Log.d("SAX","Start Element :" + qName);


                }

                public void endElement(String uri, String localName,
                                       String qName)
                        throws SAXException {

                    stack.pop();
                }

                public void characters(char ch[], int start, int length)
                        throws SAXException {

                    System.out.println(new String(ch, start, length));


                    if (title) {
                        Log.d("SAX", "End Element :" + "First Name : "
                                + new String(ch, start, length));

                        title = false;
                    }

                    if (link) {


                        Log.d("SAX", "End Element :" + "Last Name : "
                                + new String(ch, start, length));
                        link = false;
                    }

                    if (author) {

                        Log.d("SAX", "End Element :" + "Nick Name : "
                                + new String(ch, start, length));

                        author = false;
                    }

                }

            };

            saxParser.parse(is, handler);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

1 个答案:

答案 0 :(得分:0)

通常,SAX应用程序将维护堆栈以保存上下文。在startElement事件上,将元素名称推送到堆栈;在endElement上将它弹出堆栈。然后,当你获得title元素的startElement事件时,你可以执行stack.peek()来查看标题的父元素。