因为需要写个程序解析RSS,于是下载了最新的digester3版本:commons-digester3-3.2-bin.zip
http://commons.apache.org/proper/commons-logging/download_logging.cgi
用以前的惯常写法,一直不行。
public static void main(String[] args) {
Digester digester = new Digester();
hand.addRuleInstances(digester);
Channel ch = digester.parse("d:/rss.xml");
}
public void addRuleInstances(Digester dig) {
// TODO Auto-generated method stub
dig.setValidating(false) ;
dig.setNamespaceAware( true );
dig.addObjectCreate("rss/channel", Channel.class) ;
dig.addBeanPropertySetter("rss/channel/title", "title") ;
dig.addBeanPropertySetter("rss/channel/link", "link") ;
dig.addBeanPropertySetter("rss/channel/description", "description") ;
dig.addBeanPropertySetter("rss/channel/copyright", "copyright") ;
dig.addObjectCreate("rss/channel/item", Item.class) ;
dig.addBeanPropertySetter("rss/channel/item/title", "title") ;
dig.addBeanPropertySetter("rss/channel/item/link", "link") ;
dig.addBeanPropertySetter("rss/channel/item/description", "description") ;
dig.addBeanPropertySetter("rss/channel/item/pubDate", "pubDate") ;
dig.addBeanPropertySetter("rss/channel/item/guid", "guid") ;
dig.addSetNext("rss/channel/item", "addItem" ) ;
}
一直报这样一个错
java.lang.UnsupportedOperationException: This parser does not support specification “null” version “null”
查了下源码,分析原因,无外乎是说digester创建解析器失败。
自己手动建解析器,解决之
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
this.digester = new Digester(parser) ;
this.addRuleInstances(digester) ;
这样就能解决报错问题了。
附上封装的几个解析器
public Channel parse(File file ){
Channel chan = null ;
try {
InputStream in = new FileInputStream(file);
chan = this.digester.parse(in) ;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return chan ;
}
public Channel parse(String locationfile){
return this.parse(new File(locationfile)) ;
}
/**
* 解析速度受网络影响
* @param url
* @return
*/
public Channel parse(URL url){
Channel chan = null ;
try {
URLConnection feed = url.openConnection() ;
feed.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT; DigExt)");
chan = this.digester.parse(feed.getInputStream() ) ;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return chan ;
}
javabean
public class Channel {
private final List<Item> items = new ArrayList<Item>();
//@BeanPropertySetter( pattern = "rss/channel/title")
private String title;
//@BeanPropertySetter( pattern = "rss/channel/link")
private String link;
//@BeanPropertySetter( pattern = "rss/channel/description")
private String description;
//@BeanPropertySetter( pattern = "rss/channel/language")
private String language;
//@BeanPropertySetter( pattern = "rss/channel/copyright")
private String copyright ;
//@BeanPropertySetter( pattern = "rss/channel/dc:creator")
private String creator ;
//@BeanPropertySetter( pattern = "rss/channel/dc:rights")
private String rights ;
。。。