close
最近 RSS2.0 在 feed validator 做驗證時,
一直出現 Misplaced Item 的警告訊息,雖然這個錯誤不會讓 RSS 無法被 paser,
但是有警告訊息看起來就是不順眼,
Misplaced Item 提供了解決方法,
告知我的 RSS <item> 元素應該要放在 <channel> 元素的最下方,
但是使用 rome 1.0RC1 製出的 RSS feed 會少一些東西
xmlns:atom="http://www.w3.org/2005/Atom"
<atom:link
href="feed's link"
rel="self"
type="application/rss+xml"
/>
之前使用懶人寫法,直接用 dom4j 讀入 feeds 重製,
先用 dom4j 的 addNamespace() 加入 atom 的 namespace,
再用 dom4j 的 addElement() 加入atom:link的元素,
可是~addElement() 會將 Element 加到最後一個<item>之後,
大致找了一下dom4j API,都沒有 insert Element 到指定位置的寫法,
其實當使用 dom4j 取得 Node List後,就可以用 java.util.List 的方法操作 Node 了。
import com.sun.syndication.io.WireFeedOutput;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.DocumentHelper;import com.sun.syndication.feed.rss.Channel;//以下是部份程式碼Channel channel = new Channel();WireFeedOutput wf=new WireFeedOutput();Document doc=DocumentHelper.parseText(wf.outputString(channel));Element root=(Element)doc.selectSingleNode("/rss");root.addNamespace("atom","http://www.w3.org/2005/Atom");List<Element> node_list=root.element("channel").elements();Element el=DocumentHelper.createElement("atom:link").addAttribute("href","your feed link").addAttribute("rel","self").addAttribute("type","application/rss+xml");for(int i=0;i<node_list.size();i++){ if(node_list.get(i).getName().equalsIgnoreCase("image")){ node_list.add(i+1,el); }}System.out.println(doc.asXML());
全站熱搜
留言列表