RSSpect - 讀寫RSS2.0的Java類庫
}
</pre></li>
</ul>
</div>
</section> 特性
示例
RSS myRSS = new RSSDoc().readRSSToBean(new File("/myPath/myRSS.xml");
RSS myRSS = new RSSDoc().readRSSToBean(new URL("http://www.abcdefg.net/myRSS.xml");
String myRssStr = myRSS.toString();
String myRssStr = new RSSDoc().readRSSToString(myRSS, "javanet.staxutils.IndentingXMLStreamWriter");
new RSSDoc().writeRSSDoc(new File("/somewhere/myRSS.xml"), myRSS, "UTF-8", "1.0");
new RSSDoc().writeRSSDoc(new javanet.staxutils.IndentingXMLStreamWriter( XMLOutputFactory.newInstance().createXMLStreamWriter( new FileOutputStream("/somewhere/myRSS.xml"), "UTF-8")), myRSS, "UTF-8", "1.0");
public class CrudExample {
public static void main(String[] args) {
try {
RSSDoc rssDoc = new RSSDoc();
// make sure title or description is present for item
Title title = rssDoc.buildTitle("my title");
Description description = rssDoc.buildDescription("my description");
Item item = rssDoc.buildItem(title, null, description, null, null,
null, null, null, null, null, null);
// title, link and description are required for channel
// we add pubDate and items because these are usually there
Link link = rssDoc
.buildLink("http://www.colorfulsoftware.com/projects/rsspect");
List items = new ArrayList();
PubDate pubDate = rssDoc.buildPubDate(Calendar.getInstance()
.getTime().toString());
items.add(item);
Channel channel = rssDoc.buildChannel(title, link, description,
null, null, null, null, pubDate,
null, null, null, null, null, null,
null, null, null, null, null, null,
items);
//rss element require channel and version attribue
Attribute rssVersion = rssDoc.buildAttribute("version","2.0");
List attributes = new ArrayList();
attributes.add(rssVersion);
RSS rss = rssDoc.buildRSS(channel, attributes, null);
System.out.println("rss 1 : "+rss);
rssDoc.writeRSSDoc(new File("out1.xml"), rss, rssDoc.getEncoding(),rssDoc.getXmlVersion());
RSS newRSS = rssDoc.readRSSToBean(new File("out1.xml"));
System.out.println("newRSS 1 : "+newRSS);
//should print false
System.out.println("1) rss equal newRSS? "+rss.equals(newRSS));
//add the missing generator to rss
channel = rssDoc.buildChannel(channel.getTitle(), channel.getLink(), channel.getDescription(),
null, null, null, null, channel.getPubDate(),
null, null, rssDoc.getLibVersion(), null, null, null,
null, null, null, null, null, null,
items);
rss = rssDoc.buildRSS(channel, attributes, null);
System.out.println("rss 2 : "+rss);
//should print true
System.out.println("2) rss equal newRSS? "+rss.equals(newRSS));
//make an update
Attribute xmlns = rssDoc.buildAttribute("xmlns:html","http://www.w3.org/1999/xhtml");
attributes = new ArrayList();
attributes.add(rssVersion);
attributes.add(xmlns);
Extension extension = rssDoc.buildExtension("html:html", attributes,"web markupHello Web.");
List extensions = new ArrayList();
extensions.add(extension);
newRSS = rssDoc.buildRSS(channel, attributes, extensions);
System.out.println("newRSS 2 : "+newRSS);
//should print false
System.out.println("3) rss equal newRSS? "+rss.equals(newRSS));
//should still be able to write to file
rssDoc.writeRSSDoc(new File("out2.xml"), newRSS, rssDoc.getEncoding(),rssDoc.getXmlVersion());
//delete the extension
attributes = new ArrayList();
attributes.add(rssVersion);
newRSS = rssDoc.buildRSS(channel, attributes, null);
System.out.println("newRSS 3 : "+newRSS);
//should print true
System.out.println("4) rss equal newRSS? "+rss.equals(newRSS));
//another way to write the feed to file
BufferedWriter out = new BufferedWriter(new FileWriter("out3.xml"));
out.write(newRSS.toString());
out.close();
//another way to consume xml into a bean
newRSS = rssDoc.readRSSToBean(rss.toString());
System.out.println("newRSS 4 : "+newRSS);
//should print true
System.out.println("5) rss equal newRSS? "+rss.equals(newRSS));
System.out.println("finished.");
} catch (Exception e) {
System.out.println("error with feed: " + e.getMessage());
e.printStackTrace();
}
}
相關經驗
相關資訊