【发布时间】:2015-11-17 05:32:20
【问题描述】:
我正在尝试解析 rss xml,但在解析描述时卡住了,因为我的程序在遇到 (') 时会停止解析描述内容。
解析xml的代码:
public class RSSAX {
String channel_title="";
public void displayRSS()
{
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse("http://www.ronkaplansbaseballbookshelf.com/feed/podcast/", new RSSHandler());
} catch (Exception e) {
// TODO: handle exception
System.out.println("Messge is "+e.getMessage());
}
}
private class RSSHandler extends DefaultHandler
{
private boolean isItem = false;
private String tagName="";
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
this.tagName= qName;
if(qName.equals("item"))
{
this.isItem=true;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
this.tagName="";
if(qName.equals("item"))
{
System.out.println("========================");
this.isItem=false;
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if(this.isItem)
{
//System.out.println("tagname is "+this.tagName);
if(this.tagName.equals("title"))
{
System.out.println("title is "+(new String(ch,start,length)));
this.tagName="";
}
else if(this.tagName.equals("link"))
{
System.out.println("link is "+(new String(ch,start,length)));
this.tagName="";
}
else if(this.tagName.equals("description"))
{
String test=(new String(ch,start,length)).replaceAll("\\<.*?>","");
test=StringEscapeUtils.escapeXml(StringEscapeUtils.unescapeXml(test));
System.out.println("description is "+test);
this.tagName="";
}
else if(this.tagName.equals("comments"))
{
System.out.println("comment link is "+(new String(ch,start,length)));
this.tagName="";
}
else if(this.tagName.equals("pubDate"))
{
System.out.println("pubDate is "+(new String(ch,start,length)));
this.tagName="";
}
else if(this.tagName.equals("category"))
{
System.out.println("Category is "+(new String(ch,start,length)));
this.tagName="";
}
else if(this.tagName.equals("content:encoded"))
{
System.out.println("content:encoded is "+(new String(ch,start,length)));
//this.tagName="";
}
}
}
}
输出:
标题是书架对话:菲利普·邦迪
链接是http://www.ronkaplansbaseballbookshelf.com/2015/08/04/the-bookshelf-conversation-filip-bondy/
pubDate 是 2015 年 8 月 4 日星期二 14:31:45 +0000
评论链接是http://www.ronkaplansbaseballbookshelf.com/2015/08/04/the-bookshelf-conversation-filip-bondy/#comments
类别是 2015 年的标题
类别是作者简介/Ron Kaplan 的采访
description is My New Jersey 陆地人和资深体育作家 Filip Bondy 为全国消遣历史上最著名的游戏之一制作了一本有趣的书。随时随地
当遇到 there's..
【问题讨论】:
-
有什么异常?
标签: java xml parsing saxparser