简体   繁体   English

SAX解析器返回空字符串

[英]SAX Parser returning empty string

I am trying to extract data from RSS feed. 我正在尝试从RSS feed中提取数据。 RSS link - http://www.thehindu.com/sport/?service=rss ? RSS链接-http: //www.thehindu.com/sport/ ?service=rss?

Here are my default handler's character method. 这是我默认的处理程序的character方法。

public void characters(char[] ch, int start, int length) {
    String text = "";
    for (int i=0; i<length; i++)
        text += ch[start+i];

}

When I try to print the 'text' for the description tag, it comes out to be empty. 当我尝试打印描述标签的“文本”时,它显示为空。 Is there an error with the above code or is it the RSS data format that's causing the problem?? 上面的代码是否有错误,或者是引起问题的RSS数据格式?

The characters method might be invoked multiple times for a single text node better use something like this: 对于单个文本节点,可以多次调用characters方法,最好使用如下所示的方法:

private StringBuilder stringBuilder; // or Deque<StringBuilder> for nested elements

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

  if ("...".equals(qName)) {
      stringBuilder = new StringBuilder();
  }

}

public void characters(char ch[], int start, int length)  {
  if (stringBuilder != null)
     stringBuilder.append(ch, start, length);
}

public void endElement(String uri, String localName, String qName) {
  if ("...".equals(qName)){
    String s = stringBuilder.toString();
  }
  stringBuilder = null;
}

The ... is used for the value of the element containing the text node. ...用于包含文本节点的元素的值。 Depending on you namespace use, you might have to use localName as apposed to qName ) 根据命名空间的使用,您可能必须使用与qName localName

It isn't clear how we are getting to here from the SAX representation of the RSS; 目前尚不清楚我们如何从RSS的SAX表示到达这里。 Or, for that matter, what you have done to validate that you got to the URL, fetched and parsed some RSS. 或者,就此而言,您要做的是验证您是否到达URL,获取并解析一些RSS。

But this method seems to do what the Java API can do in a String constructor: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String%28char[],%20int,%20int%29 但是此方法似乎可以完成Java API在String构造函数中的工作: http : //docs.oracle.com/javase/7/docs/api/java/lang/String.html#String%28char[],% 20int,%20int%29

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM