<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 262,  comments - 221,  trackbacks - 0
    Informa不僅提供了對不同版本的RSS Feed source的讀入和解析,同樣也提供了將channel object導出為不同協議版本的XML文件的功能。這個功能是通過exporters包下的各個導出類來完成的。目前僅支持對RSS協議的導出,不支持Atom協議、OPML協議的導出。

    所有的導出類都實現了ChannelExporterIF接口
    /**
     * A channel exporter is used to write channel objects in an
     * implementation dependent way to an implicit destination.</p>
     *
     * 
    @author Niko Schmuck (niko@nava.de) 
     
    */

    public interface ChannelExporterIF {

      
    /**
       * Writes the given channel to an implicit implementation dependent
       * destination.
       *
       * 
    @param channel - The channel to be exported/written;
       *                  an object implementing ChannelIF.
       * 
    @throws IOException - Thrown if writing the channel fails.
       
    */

      
    void write(ChannelIF channel) throws IOException;

    }

    ★RSS_0_91_Exporter

    RSS_0_91_Exporter中提供了幾種構造方法
    public RSS_0_91_Exporter(String filename) throws IOException {
        
    this(new File(filename), "utf-8");
    }


    public RSS_0_91_Exporter(File file) throws IOException {
        
    this(file, "utf-8");
    }


    public RSS_0_91_Exporter(File file, String encoding) throws IOException {
        
    this.writer = new OutputStreamWriter(new FileOutputStream(file),
        encoding);
        
    this.encoding = encoding;
    }


    public RSS_0_91_Exporter(Writer writer, String encoding) {
        
    this.writer = writer;
        
    this.encoding = encoding;
    }

    真正的生成XML文件的過程其實很簡單,就是按照RSS 0.9.1規范的格式將channel object的屬性映射到XML文件的element去。
            // create items for channel
            Collection<ItemIF> items = channel.getItems();
            Iterator
    <ItemIF> it = items.iterator();
            
    while (it.hasNext()) {
                ItemIF item 
    = (ItemIF) it.next();
                Element itemElem 
    = new Element("item");
                itemElem.addContent(
    new Element("title").
                        setText(item.getTitle()));
                
    if (item.getLink() != null{
                    itemElem.addContent(
    new Element("link").
                            setText(item.getLink().toString()));
                }

                
    if (item.getDescription() != null{
                    itemElem.addContent(
    new Element("description").
                            setText(item.getDescription()));
                }

                
    if (item.getDate() != null{
                    itemElem.addContent(
    new Element("pubDate").
                            setText(ParserUtils.formatDate(item.getDate())));
                }

                channelElem.addContent(itemElem);
            }

    這是write(Channel)方法的其中一個片段,是用于導出channel中的item節點的。

    RSS_2_0_Exporter

    和RSS 0.9.1的導出類似,2.0版本的導出同樣需要實現ChannelExporterIF接口,同樣提供了和上面一樣參數類型的構造方法。不同的是在2.0的版本,由于新增了幾個定義,所以增加了對應的構建方法。例如下面的getCategoryElements方法

        // ------------------------------------------------------------
        
    // Build a hierarchical category String from CategoryIF
        
    // ------------------------------------------------------------

        
    private Element getCategoryElements(Element elem, CategoryIF category,
                StringBuffer catString) 
    {
            StringBuffer l_catString;
            
    if (catString == null || catString.length() < 1)
                l_catString = new StringBuffer(category.getTitle());

            
    else
                l_catString = catString.append("/").append(category.getTitle());


            Collection
    <CategoryIF> categories = category.getChildren();
            
    if (categories.size() == 0{
                elem.addContent(
    new Element("category").setText(l_catString
                        .toString()));
            }
     else {
                Iterator
    <CategoryIF> catIt = categories.iterator();
                
    while (catIt.hasNext()) {
                    CategoryIF childCat 
    = (CategoryIF) catIt.next();
                    elem = getCategoryElements(elem, childCat, l_catString);

                }

            }

            
    return elem;
        }

    這個方法采用遞歸的方式,獲取一個channel所屬于的category,因為在Category對象中,關系是以級聯的方式存在的,要把這種對象的級聯關系展開為平行的字符串,只能通過遞歸的方式訪問到最底層的category對象,得到其title后,再用分隔符"/"拼接后返回給上一層。

    在2.0的write方法中,新增了對Namespace的支持
     Namespace dcNs = Namespace.getNamespace("dc", NS_DC);
     Namespace syNs 
    = Namespace.getNamespace("sy", NS_SY);
     Namespace adminNs 
    = Namespace.getNamespace("admin", NS_ADMIN);

    其余的生成過程和0.9.1的基本相同,除了在category方面
            if (channel.getCategories() != null{
                Collection<CategoryIF> categories = channel.getCategories();

                Iterator
    <CategoryIF> catIt = categories.iterator();
                
    while (catIt.hasNext()) {
                    CategoryIF cat 
    = (CategoryIF) catIt.next();
                    channelElem = getCategoryElements(channelElem, cat, null);

                }

            }

    RSS_1_0_Exporter

    RSS_1_0_Exporter導出和0.9.1和2.0協議的過程有些不同,區別在于item方面和部分屬性,這里重點介紹一下item方面的導出過程
            // ===========================================
            Element itemsElem = new Element("items", defNs);
            Element seqElem 
    = new Element("Seq", rdfNs);
            Collection
    <ItemIF> items = channel.getItems();
            Iterator
    <ItemIF> it = items.iterator();
            
    while (it.hasNext()) {
                ItemIF item 
    = (ItemIF) it.next();
                Element itemElem 
    = new Element("li", rdfNs);
                
    if (item.getLink() != null{
                    itemElem.setAttribute(
    "resource", item.getLink().toString());
                }

                seqElem.addContent(itemElem);
            }

            itemsElem.addContent(seqElem);
            channelElem.addContent(itemsElem);
            rootElem.addContent(channelElem);

            
    // item-by-item en detail
            items = channel.getItems();
            it 
    = items.iterator();
            
    while (it.hasNext()) {
                ItemIF item 
    = (ItemIF) it.next();
                Element itemElem 
    = new Element("item", defNs);
                
    if (item.getLink() != null{
                    itemElem.setAttribute(
    "about"
                        item.getLink().toString(), rdfNs);
                }

                itemElem.addContent(
    new Element("title", defNs).
                        setText(item.getTitle()));
                
    if (item.getLink() != null{
                    itemElem.addContent(
    new Element("link", defNs).
                        setText(item.getLink().toString()));
                }

                
    if (item.getDescription() != null{
                    itemElem.addContent(
    new Element("description", dcNs).
                        setText(item.getDescription()));
                }

                
    if (item.getDate() != null{
                    itemElem.addContent(
    new Element("date", dcNs).
                        setText(ParserUtils.formatDate(item.getDate())));
                }


                rootElem.addContent(itemElem);
            }

    和0.9.1和2.0協議不同,1.0的RSS對于item的描述有2次,第一次是summary,第二次是detail。
    <items>
          
    <rdf:Seq>
            
    <rdf:li resource="http://xml.com/pub/2000/08/09/xslt/xslt.html" />
            
    <rdf:li resource="http://xml.com/pub/2000/08/09/rdfdb/index.html" />
          
    </rdf:Seq>
        
    </items>

    <item rdf:about="http://xml.com/pub/2000/08/09/xslt/xslt.html">
        
    <title>Processing Inclusions with XSLT</title>
        
    <link>http://xml.com/pub/2000/08/09/xslt/xslt.html</link>
        
    <description>
         Processing document inclusions with general XML tools can be 
         problematic. This article proposes a way of preserving inclusion 
         information through SAX-based processing.
        
    </description>
      
    </item>
      
      
    <item rdf:about="http://xml.com/pub/2000/08/09/rdfdb/index.html">
        
    <title>Putting RDF to Work</title>
        
    <link>http://xml.com/pub/2000/08/09/rdfdb/index.html</link>
        
    <description>
         Tool and API support for the Resource Description Framework 
         is slowly coming of age. Edd Dumbill takes a look at RDFDB, 
         one of the most exciting new RDF toolkits.
        
    </description>
      
    </item>

    可以看到上面的<items>是大概地告知了RSS閱讀器這個channel下面有多少個item,順序如何。下面每一個<item>則是對上面提到的item的詳細描述。根據規范,<item>節點的rdf:about屬性值必須和rdf:li的resource屬性值一樣。

    上面的代碼就是先找出item的列表,構建<items>節點及其子節點<rdf:seq>,后面是構建<item>節點。所以有2次的節點迭代操作。


    -------------------------------------------------------------
    生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
    posted on 2010-01-04 10:15 Paul Lin 閱讀(406) 評論(0)  編輯  收藏 所屬分類: J2SE
    <2010年1月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    常用鏈接

    留言簿(21)

    隨筆分類

    隨筆檔案

    BlogJava熱點博客

    好友博客

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲欧美日韩综合久久久久| 国产av无码专区亚洲av果冻传媒| 91亚洲精品视频| 国产免费无码一区二区| 国产亚洲情侣一区二区无| 一级看片免费视频| 久久国产成人精品国产成人亚洲| 国产久爱免费精品视频| 国产精品亚洲二区在线观看| 国产精品免费观看视频| 国产亚洲成人在线播放va| 91国内免费在线视频| 亚洲AV无码专区国产乱码电影 | 免费观看无遮挡www的视频| 亚洲首页在线观看| 成年在线网站免费观看无广告| 亚洲免费综合色在线视频| 四虎免费永久在线播放| 五月婷婷免费视频| 亚洲电影中文字幕| 亚洲国产精品免费观看| 国产精品亚洲色图| 亚洲色欲色欲www在线丝| 久久精品无码专区免费青青| 亚洲 日韩 色 图网站| 日韩精品亚洲专区在线观看| 国产精品美女久久久免费| 亚洲激情电影在线| 在线观看免费亚洲| 一个人免费视频在线观看www| 亚洲人成亚洲精品| 在线观看91精品国产不卡免费| 两性色午夜视频免费播放| 亚洲人成7777影视在线观看| 国产一级淫片视频免费看| 暖暖免费日本在线中文| 亚洲精品无码你懂的| 亚洲成AV人片在线观看无| 永久黄网站色视频免费直播| 三年片免费高清版| 亚洲色偷偷偷综合网|