posts - 262,  comments - 221,  trackbacks - 0

一个RSS频道通常被我们叫做Feed,假如现在我们收集的RSS Feed越来越多,我想分享出去。那么可以做的办法有什么呢?手工把这些Feed的地址一个个copy出来,然后发送吗?这样太麻烦了。

OPML(Outline Processor Markup Language)协议正是用于解决这样问题的。利用OPML协议,我们可以通过阅读器把已经订阅的频道导出为OPML协议规定的格式,从而作为
一个文件分享出去。

Informa中同样提供了对于OPML文件的解析,目前支持的版本是1.1版本。

★OPMLParser



OPMLParser中同样提供了类似FeedParser的多种数据源支持的解析,可以分别从URL,文件,字节流,字符流中读入OPML文件,这个类的核心方法是parse(Dcoument)。

    private static synchronized Collection<FeedIF> parse(Document doc)
            
throws ParseException {

        logger.debug(
"start parsing.");
        
// Get the root element (must be opml)
        Element root = doc.getRootElement();
        String rootElement 
= root.getName().toLowerCase();
        
// Decide which parser to use
        if (rootElement.startsWith("opml")) {
            String opmlVersion 
= root.getAttribute("version").getValue();
            
if (opmlVersion.indexOf("1.1">= 0{
                logger.info(
"Collection uses OPML root element (Version 1.1).");
                
return OPML_1_1_Parser.parse(root);
            }

        }


        
// did not match anything
        throw new UnsupportedFormatException("Unsupported OPML root element ["
                
+ rootElement + "].");
    }


可以看到这个类又是一个中介类,简单地分析协议的版本然后委托给相应的解析器。目前由于OPML协议解析器只支持1.1版本,所以只有一个OPML_1_1_Parser

★OPML_1_1_Parser

    static Collection<FeedIF> parse(Element root) {

        Collection
<FeedIF> feedColl = new ArrayList<FeedIF>();

        Date dateParsed 
= new Date();
        logger.debug(
"start parsing.");

        
// Lower the case of these tags to simulate case-insensitive parsing
        ParserUtils.matchCaseOfChildren(root, "body");

        
// Get the head element (only one should occur)
        
//    Element headElem = root.getChild("head");
        
//    String title = headElem.getChildTextTrim("title");

        
// Get the body element (only one occurs)
        Element bodyElem = root.getChild("body");

        
// 1..n outline elements
        ParserUtils.matchCaseOfChildren(bodyElem, "outline");
        List feeds 
= bodyElem.getChildren("outline");
        Iterator i 
= feeds.iterator();
        
while (i.hasNext()) {
            Element feedElem 
= (Element) i.next();
            
// get title attribute
            Attribute attrTitle = feedElem.getAttribute("title");
            String strTitle 
= "[No Title]";
            
if (attrTitle != null{
                strTitle 
= attrTitle.getValue();
            }

            FeedIF feed 
= new Feed(strTitle);
            
if (logger.isDebugEnabled()) {
                logger.debug(
"Feed element found (" + strTitle + ").");
            }

            
// get text attribute
            Attribute attrText = feedElem.getAttribute("text");
            String strText 
= "[No Text]";
            
if (attrText != null{
                strText 
= attrText.getValue();
            }

            feed.setText(strText);
            
// get attribute type (for example: 'rss')
            Attribute attrType = feedElem.getAttribute("type");
            String strType 
= "text/xml";
            
if (attrType != null{
                strType 
= attrType.getValue();
            }

            feed.setContentType(strType);

            
// TODO: handle attribute version (for example: 'RSS')

            
// get attribute xmlUrl
            Attribute attrXmlUrl = feedElem.getAttribute("xmlUrl");
            
if (attrXmlUrl != null && attrXmlUrl.getValue() != null{
                feed.setLocation(ParserUtils.getURL(attrXmlUrl.getValue()));
            }

            
// get attribute htmllUrl
            Attribute attrHtmlUrl = feedElem.getAttribute("htmlUrl");
            
if (attrHtmlUrl != null && attrHtmlUrl.getValue() != null{
                feed.setSite(ParserUtils.getURL(attrHtmlUrl.getValue()));
            }

            
// set current date
            feed.setDateFound(dateParsed);
            
// add feed to collection
            feedColl.add(feed);
        }


        
return feedColl;
    }


OPML协议的结构如下:

 A.根节点是<opml version="1.x">元素
 B.子节点<head>元素,该元素包含了title,dateCreated,dateModified,ownerName等属性
 C.子节点<body>元素,该元素包含了0..若干个<outline>元素,该元素是可嵌套的



-------------------------------------------------------------
生活就像打牌,不是要抓一手好牌,而是要尽力打好一手烂牌。
posted on 2010-01-02 20:56 Paul Lin 阅读(329) 评论(0)  编辑  收藏 所属分类: J2SE

只有注册用户登录后才能发表评论。


网站导航:
 
<2010年1月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(21)

随笔分类

随笔档案

BlogJava热点博客

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜