【问题标题】:Sax parser takes wrong tag AndroidSax 解析器采用错误的标签 Android
【发布时间】:2014-03-11 06:22:56
【问题描述】:

下面是我的 Saxparser 类,用于检索标记名称“item”中的所有元素。

   class SAXHelper {
      public HashMap<String, String> userList = new HashMap<String, String>();
      private URL url2;

      public SAXHelper(String url1) throws MalformedURLException {
       this.url2 = new URL(url1);
      }

      public RSSHandler parseContent(String parseContent) {
       RSSHandler df = new RSSHandler();
       try {

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        xr.setContentHandler(df);
        xr.parse(new InputSource(url2.openStream()));
       } catch (Exception e) {
        e.printStackTrace();
       }
       return df;
      }
     }

     class RSSHandler extends DefaultHandler {

      private Post currentPost = new Post();
      StringBuffer chars = new StringBuffer();

      @Override
      public void startElement(String uri, String localName, String qName,
        Attributes atts) {

       chars = new StringBuffer();

       if (localName.equalsIgnoreCase("item")) {

       }
      }

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

       if (localName.equalsIgnoreCase("title")
         && currentPost.getTitle() == null) {
        currentPost.setTitle(chars.toString());

        System.out.println("title1: " + currentPost.getTitle());

       }
       if (localName.equalsIgnoreCase("category")
         && currentPost.getCategory() == null) {
        currentPost.setCategory(chars.toString());

        System.out.println("category: " + currentPost.getCategory());

       }

       if (localName.equalsIgnoreCase("description")
         && currentPost.getDescription() == null) {
        currentPost.setDescription(chars.toString());

       }

       if (localName.equalsIgnoreCase("link")
         && currentPost.getLink() == null) {
        currentPost.setLink(chars.toString());

        System.out.println("link: " + currentPost.getLink());

       }

       if (localName.equalsIgnoreCase("pubDate")
         && currentPost.getPubDate() == null) {
        currentPost.setPubDate(chars.toString());

        String x = currentPost.getPubDate();
        String last = x.substring(0, x.length() - 4);
        int start = last.length() - 8;
        int end = last.length() - 3;
        String result = x.substring(start, end);

        result = Stringreplace(result);
       }

       if (localName.equalsIgnoreCase("item")) {
        PostListNormal.add(currentPost);
        currentPost = new Post();
       }

      }

      @Override
      public void characters(char ch[], int start, int length) {
       chars.append(new String(ch, start, length));
      }

     }

这里是要提取的xml标签。

   <?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
      <channel>
        <title>matches en direct</title>
        <link>http://www.match.com/</link>
        <atom:link href="http://www.match.com/news,60,0,UTF-8,fulltext.rss" rel="self" type="application/rss+xml" />
        <description>Foot 01 - N°1 de l'actu du match, du mercato et des matches en direct</description>
        <language>fr-FR</language>
        <pubDate>Tue, 11 Mar 2014 06:12:38 GMT</pubDate>
        <lastBuildDate>Tue, 11 Mar 2014 06:12:38 GMT</lastBuildDate>
        <category>Sports</category>
        <copyright>© 2011 match.com</copyright>
        <ttl>1</ttl>


        <item>
          <category>Espagne, match </category>
          <title>a</title>
          <link>aaaa</link>
          <guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
          <description>
            match progress
          </description>
          <pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
        </item>

            <item>
          <category>x, match </category>
          <title>a</title>
          <link>aaaa</link>
          <guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
          <description>
            match progress
          </description>
          <pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
        </item>

                <item>
          <category>x, match </category>
          <title>a</title>
          <link>aaaa</link>
          <guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
          <description>
            match progress
          </description>
          <pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
        </item>

                    <item>
          <category>x, match </category>
          <title>a</title>
          <link>aaaa</link>
          <guid isPermaLink="true">http://www.match.com/foot-europeen/espagne/27e-j-les-match-definitifs,138131</guid>
          <description>
            match progress
          </description>
          <pubDate>Mon, 10 Mar 2014 22:52:02 GMT</pubDate>
        </item>

      </channel>
      </rss>

当我运行代码时,我的结果是标题标签

直接匹配, 一种, 一种, 一个。

预期结果应该是 4 个“a”元素。

简而言之,打印通道标签中的标题,然后忽略第一个“item”标签,最后打印第二个标签项目之后的所有标签项目。

我怎样才能打印所有标签项目只有任何想法。对不起我的英语不好。

【问题讨论】:

  • 所以,你的问题是,解析器应该解析 4 个标题元素,但它解析 3 个标题元素......我说的对吗?
  • 它应该只传递“item”标签中的所有标签元素,而忽略频道中的独立标签元素。

标签: android xml-parsing saxparser


【解决方案1】:

维护一个boolean 以识别item 中的当前元素,如下所示...

class RSSHandler extends DefaultHandler {

    private Post currentPost = new Post();
    StringBuffer chars = new StringBuffer();

    boolean isItem = false;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) {

        chars = new StringBuffer();

        if (localName.equalsIgnoreCase("item")) {

            isItem = true;

        }
    }

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

        if (localName.equalsIgnoreCase("title") && currentPost.getTitle() == null && isItem == true) {
            currentPost.setTitle(chars.toString());

            System.out.println("title1: " + currentPost.getTitle());

        }
        if (localName.equalsIgnoreCase("category") && currentPost.getCategory() == null) {
            currentPost.setCategory(chars.toString());

            System.out.println("category: " + currentPost.getCategory());

        }

        if (localName.equalsIgnoreCase("description") && currentPost.getDescription() == null) {
            currentPost.setDescription(chars.toString());

        }

        if (localName.equalsIgnoreCase("link") && currentPost.getLink() == null) {
            currentPost.setLink(chars.toString());

            System.out.println("link: " + currentPost.getLink());

        }

        if (localName.equalsIgnoreCase("pubDate") && currentPost.getPubDate() == null) {
            currentPost.setPubDate(chars.toString());

            String x = currentPost.getPubDate();
            String last = x.substring(0, x.length() - 4);
            int start = last.length() - 8;
            int end = last.length() - 3;
            String result = x.substring(start, end);

            result = Stringreplace(result);
        }

        if (localName.equalsIgnoreCase("item")) {
            PostListNormal.add(currentPost);
            currentPost = new Post();

            isItem = false;
        }

    }

    @Override
    public void characters(char ch[], int start, int length) {
        chars.append(new String(ch, start, length));
    }

}

【讨论】:

  • 用这个类替换你的整个RSSHandler 类...然后说,是否可以。
  • @Dimitri ...很高兴收到您的来信...那么请接受投票并接受答案。 :)
  • 是的,但最后一个问题,比如我想打印描述值,它是否也会从通道中获取描述,或者仅从项目中获取描述,这是预期的结果。代码是否仅适用于标题或所有其他标签,因为我从日志中的描述中获取值。谢谢:)
  • 如果您不应用与boolean 相同的条件,那么channeldescription 将被解析...应用与title 中的title 相同的条件@。
猜你喜欢
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-05-26
  • 2013-12-05
  • 2015-02-15
  • 1970-01-01
  • 2013-02-08
相关资源
最近更新 更多