Java 为什么在解析RSS提要时必须两次初始化对象
在MainActivity.java中,我编写了解析代码,如Java 为什么在解析RSS提要时必须两次初始化对象,java,android,rss,saxparser,Java,Android,Rss,Saxparser,在MainActivity.java中,我编写了解析代码,如 public void parser() { try { /****** Creating a new instance of the SAX parser ****************/ SAXParserFactory saxPF = SAXParserFactory.newInstance(); SAXParser saxParser = saxPF.newSAXPar
public void parser() {
try {
/****** Creating a new instance of the SAX parser ****************/
SAXParserFactory saxPF = SAXParserFactory.newInstance();
SAXParser saxParser = saxPF.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
URL url = new URL("http://ibnlive.in.com/ibnrss/rss/world/world.xml");
myXMLHandler = new FeedsXMLHandler();
xmlReader.setContentHandler(myXMLHandler);
xmlReader.parse(new InputSource(url.openStream()));
} catch (Exception e) {
e.printStackTrace();
}
feedsData = myXMLHandler.getXMLData();
}
我正在使用IBN Live的rss源
我的处理程序类XMLHandler.java如下所示:
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementValue = "";
elementOn = true;
if (localName.equalsIgnoreCase("rss")) {
dataArray = new ArrayList<FeedsItems>();
data = new FeedsItems();
Log.v("Item", "I am in rss block");
}
if (localName.equalsIgnoreCase("channel")) {
Log.v("Item", "I am in channel block");
}
if (localName.equalsIgnoreCase("item")) {
data = new FeedsItems();
Log.v("Item", "I am in item block");
}
if (localName.equalsIgnoreCase("description")) {
bufferDesc = new StringBuffer();
elementOn = true;
}
if (localName.equalsIgnoreCase("title")) {
bufferTitle = new StringBuffer();
elementOn = true;
}
if (localName.equalsIgnoreCase("link")) {
bufferLink = new StringBuffer();
elementOn = true;
}
}
/*********** Method will be called when the tags of the XML end **************/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
elementOn = false;
/*** Sets the values after retrieving the values from the XML tags ******/
if (localName.equalsIgnoreCase("title")) {
elementOn = false;
data.setTitle(bufferTitle.toString());
bufferTitle.delete(0, bufferTitle.length());
Log.v("title", data.getTitle());
}
else if (localName.equalsIgnoreCase("link")) {
elementOn = false;
data.setFeedsUrl(bufferLink.toString());
bufferLink.delete(0, bufferLink.length());
}
else if (localName.equalsIgnoreCase("description")) {
elementOn = false;
data.setDescription(bufferDesc.toString());
bufferDesc.delete(0, bufferDesc.length());
} else if (localName.equalsIgnoreCase("item")) {
dataArray.add(data);
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (elementOn) {
if (bufferDesc != null) {
bufferDesc.append(new String(ch, start, length).trim());
}
if (bufferTitle != null) {
bufferTitle.append(new String(ch, start, length).trim());
}
if (bufferLink != null) {
bufferLink.append(new String(ch, start, length).trim());
}
} else {
elementValue = new String(ch, start, length);
}
}
试试这个
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
elementValue = "";
elementOn = true;
if (localName.equalsIgnoreCase("rss")) {
dataArray = new ArrayList<FeedsItems>();
data = new FeedsItems();
bufferLink = new StringBuffer();
bufferDesc = new StringBuffer();
bufferTitle = new StringBuffer();
Log.v("Item", "I am in rss block");
}
else if (localName.equalsIgnoreCase("channel")) {
Log.v("Item", "I am in channel block");
}
else if (localName.equalsIgnoreCase("item")) {
Log.v("Item", "I am in item block");
}
else if (localName.equalsIgnoreCase("description")) {
elementOn = true;
}
else if (localName.equalsIgnoreCase("title")) {
elementOn = true;
}
else if (localName.equalsIgnoreCase("link")) {
elementOn = true;
}
}
@覆盖
public void startElement(字符串uri、字符串localName、字符串qName、,
属性)引发SAX异常{
elementValue=“”;
elementOn=true;
if(localName.equalsIgnoreCase(“rss”)){
dataArray=新的ArrayList();
数据=新的FeedsItems();
bufferLink=新的StringBuffer();
bufferDesc=新的StringBuffer();
bufferTitle=新的StringBuffer();
Log.v(“项目”,“我在rss块中”);
}
else if(localName.equalsIgnoreCase(“通道”)){
Log.v(“项目”,“我在通道区”);
}
else if(localName.equalsIgnoreCase(“项”){
Log.v(“项目”,“我在项目块中”);
}
else if(localName.equalsIgnoreCase(“description”){
elementOn=true;
}
else if(localName.equalsIgnoreCase(“title”)){
elementOn=true;
}
else if(localName.equalsIgnoreCase(“link”){
elementOn=true;
}
}
您必须执行两次,因为
和
(以及
)都使用
标记。如果你不把它实例化
if (localName.equalsIgnoreCase("rss")) {
...
}
当您在第一个
您要做的是保留这两个元素,或者删除“rss”的一个元素,并在endElement()中添加一个检查项,确保标题属于
,而不是任何其他元素。在FeedsXMLHandler.java
中的行号83是什么?您可以发布一个示例,说明您试图解析的rss提要是什么样子的吗?也许有些元素您不希望在其中找到它们。@Tamilan:行号83是data.setTitle(bufferTitle.toString());(在endElement(…)方法中)@TofferJ:这是rss的urlfeed@user3022836检查我的答案。。
if (localName.equalsIgnoreCase("rss")) {
...
}