本文介绍了Android的SAX解析器没有从标签之间全文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的DefaultHandler解析RSS源,并为大多数饲料它不过做工精细,为ESPN,它切断了文章的URL的一部分,由于道路ESPN的格式,它的网址。来自ESPN的一个完整的文章的URL的一个例子。

<$p$p><$c$c>http://sports.espn.go.com/nba/news/story?id=5189101&amp;campaign=rss&amp;source=ESPNHeadlines

现在的问题是由于某种原因的DefaultHandler字符方法仅获得这个从包含上面的URL标记。

  http://sports.espn.go.com/nba/news/story?id=5189101
 

正如你所看到的,它的切割过的一切从符号逃生code和之后的URL。我怎样才能获得SAX解析器在这个逃生code不把我串了吗?对于裁判。这里是我的角色的方法。

 公共无效字符(字符CH [],诠释开始,诠释长度){

  字符串的字符=(新的String(CH).substring(启动,启动+长度));

  尝试 {
   //如果没有项目,然后标题/链接指向喂
   如果(!inItem){
    如果(INTITLE)
     currentFeed.title =字符;
   } 其他 {
    如果(内链接)
     currentArticle.url =新的URL(字);
    如果(INTITLE)
     currentArticle.title =字符;
    如果(inDescription)
     currentArticle.description =字符;
    如果(inPubDate)
     currentArticle.pubDate =字符;
    如果(inEnclosure){
    }
   }
  }赶上(MalformedURLException异常E){
   Log.e(RSSReader,e.toString());
  }
 }
 

罗布W的

解决方案

在documentation的字符()方法:

在我写的SAX解析器,我用的是的StringBuilder 追加传递给一切字符()

 公共无效字符(字符CH [],诠释开始,诠释长度){
    如果(BUF!= NULL){
        的for(int i =启动; I&LT;启动+长度;我++){
            buf.append(CH [i]);
        }
    }
}
 

然后在的endElement(),我走了的StringBuilder 的内容,并用它做什么。这样一来,如果语法分析器调用字符()好几次,我不会错过任何东西。

I've created my own DefaultHandler to parse rss feeds and for most feeds it's working fine, however, for ESPN, it is cutting off part of the article url due to the way ESPN formats it's urls. An example of a full article url from ESPN..

http://sports.espn.go.com/nba/news/story?id=5189101&amp;campaign=rss&amp;source=ESPNHeadlines

The problem is for some reason the DefaultHandler characters method is only getting this from the tag that contains the above url.

http://sports.espn.go.com/nba/news/story?id=5189101

As you can see, it's cutting everything off the url from the ampersand escape code and after. How can I get the SAX parser to not cut my string off at this escape code? For ref. here is my characters method..

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

  String chars = (new String(ch).substring(start, start + length));

  try {
   // If not in item, then title/link refers to feed
   if (!inItem) {
    if (inTitle)
     currentFeed.title = chars;
   } else {
    if (inLink)
     currentArticle.url = new URL(chars);
    if (inTitle)
     currentArticle.title = chars;
    if (inDescription)
     currentArticle.description = chars;
    if (inPubDate)
     currentArticle.pubDate = chars;
    if (inEnclosure) {
    }
   }
  } catch (MalformedURLException e) {
   Log.e("RSSReader", e.toString());
  }
 }

Rob W.

解决方案

From the documentation of the characters() method:

When I write SAX parsers, I use a StringBuilder to append everything passed to characters():

public void characters (char ch[], int start, int length) {
    if (buf!=null) {
        for (int i=start; i<start+length; i++) {
            buf.append(ch[i]);
        }
    }
}

Then in endElement(), I take the contents of the StringBuilder and do something with it. That way, if the parser calls characters() several times, I don't miss anything.

这篇关于Android的SAX解析器没有从标签之间全文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 01:36