RSS reader for Web (1) -- dom4j reads XML (opml) files

Source: Internet
Author: User

Soon after I got started with Java, I had some occasional gains. Recently I wanted to develop a web-based RSS reader. There are several opml files exported from different versions of Foxmail. You should know that the opml file is in XML format. First, start by reading the XML file using dom4j.


In Java programming, especially Java Web development programs, XML applications are frequently used. Various web frameworks such as spring, hibernate, and struts, such as myeclipse and Oracle, all rely mainly on XML. XML plays a vital role in system configuration. These also enhance the flexibility of the system.


Let's talk about the following ideas:

Create a Java Web project, but JSP and Servlet are not used at the moment. This article only uses the built-in debugger to test and read XML. In the following blog, we will show you the optimized interfaces and provide most of the RSS reader functions.


The file format is slightly different because it is exported from different versions of Foxmail. The main difference lies in the subscription grouping function. Some versions export the group information in the head/Title content, and the body/outline stores all the subscription information; some imported group information is in the title and text attributes of the body/outline, while the detailed subscription information is in the body/outline.


The system I Want To Do can support reading multiple opml files, so I need an RSS file list configuration file "rss_config.xml", which corresponds to an object: rssconfigbean. java, which contains the opml file path information. The group information must also be separated and named rssteambean. java, including the title and text attributes and a list of subscription information. The subscription information must be independent. It is named "rssbean. Java" and includes the following attributes: Text, title, xmlurl, htmlurl, version, and type.


First, read rss_config.xml, get all opml file paths, read opml cyclically, get the group information and all the detailed subscription information under each group, and save it to the object for display by calling.


Simply put, use the Code directly:

①. Opml File

[Single group foxmail6.5.opml]

<? XML version = "1.0"?> <Opml version = "1.1"> 

[Group foxmail7.opml]

<? XML version = "1.0" encoding = "UTF-8"?> <Opml version = "1.0"> 

②. Create an RSS file list configuration file in SRC

[Rss_config.xml]

<? XML version = "1.0" encoding = "UTF-8"?> <RSS> <RSS-List> <RSS-Name> single group foxmail6.5 </RSS-Name> <RSS-path> \ RSS \ single group foxmail6.5.opml </RSS-path> </RSS-List> <RSS-Name> multi-group foxmail7 </RSS-Name> <RSS-path> \ RSS \ multi-group foxmail7.opml </RSS-Path> </RSS-List> </RSS>

③. Create the com. tgb. rssreader. Bean package and add three java files:

[Rssbean. Java] Detailed subscription information

Package COM. tgb. rssreader. bean;/*** detailed subscription information * @ author longxuan **/public class rssbean {/*** display name */private string text; /*** title */private String title;/*** RSS subscription address */private string htmlurl;/*** RSS subscription address */private string xmlurl; /*** version */private string version;/*** type */private string type; Public String gettext () {return text;} public void settext (string text) {This. TEXT = text;} Public String gettitle () {return title;} public void settitle (String title) {This. title = title;} Public String gethtmlurl () {return htmlurl;} public void sethtmlurl (string htmlurl) {this.html url = htmlurl;} Public String getxmlurl () {return xmlurl ;} public void setxmlurl (string xmlurl) {This. xmlurl = xmlurl;} Public String getversion () {return version;} public void setversion (string version) {This. version = version;} Public String GetType () {return type;} public void settype (string type) {This. type = type ;}}

[Rssconfigbean. Java] RSS configuration information

Package COM. tgb. rssreader. bean;/*** RSS configuration information * @ author longxuan **/public class rssconfigbean {/*** group name */private string name; /*** path */private string path; Public String getpath () {return path;} Public String getname () {return name;} public void setname (string name) {This. name = Name;} public void setpath (string path) {This. path = path ;}}

[Rssteambean. Java] RSS group information

Package COM. tgb. rssreader. bean; import Java. util. list;/*** RSS group information * @ author longxuan **/public class rssteambean {/*** group title */private String title; /*** group name */private string text; private list <rssbean> rssbeanlist; Public String gettitle () {return title;} public void settitle (String title) {This. title = title;} Public String gettext () {return text;} public void settext (string text) {This. TEXT = text;} public list <rssbean> getrssbeanlist () {return rssbeanlist;} public void setrssbeanlist (list <rssbean> rssbeanlist) {This. rssbeanlist = rssbeanlist ;}}

④. Create a package com. tgb. rssreader. Manager and add two files:

Rssconfigmgr. Java: RSS file list Configuration Manager

Package COM. tgb. rssreader. manager; import Java. io. inputstream; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. element; import Org. dom4j. io. saxreader; import COM. tgb. rssreader. bean. rssconfigbean;/*** RSS file list Configuration Manager * @ author longxuan **/public class rssconfigmgr {/*** read RSS file list configuration information * @ return */publ IC list <rssconfigbean> getrssconfig () {list <rssconfigbean> List = new arraylist <rssconfigbean> (); rssconfigbean = NULL; inputstream is = thread. currentthread (). getcontextclassloader (). getresourceasstream ("rss_config.xml"); If (is = NULL) {// system. out. println ("this file cannot be found"); // return NULL; throw new runtimeexception ("rss_config.xml file not found ");} try {// read and parse the XML document // saxreader is a pipeline that reads XML files in a stream. Saxreader reader = new saxreader (); // The following is the document DOC = reader by parsing the XML string. read (is); element rootelt = Doc. getrootelement (); // get the root node // system. out. println ("root node:" + rootelt. getname (); // get the name of the root node iterator <?> Iter = rootelt. elementiterator ("RSS-List"); // obtain the subnode RSS-list under the root node // traverse the RSS-list node while (ITER. hasnext () {element recordele = (element) ITER. next (); string name = recordele. elementtexttrim ("RSS-name"); // obtain the name of the subnode under the RSS-list node. // system. out. println ("name:" + name); string Path = recordele. elementtexttrim ("RSS-path"); // obtain the PATH value of the subnode under the RSS-list node. // system. out. println ("Path:" + path); rssconfigbean = new rssconfigbean (); // save it to rssconfigbean. setname (name); rssconfigbean. setpath (PATH); // save it to list. add (rssconfigbean) ;}} catch (incluentexception e) {e. printstacktrace ();} return list ;}}

[Readxml. Java] reads XML files.

Package COM. tgb. rssreader. manager; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. util. arraylist; import Java. util. iterator; import Java. util. list; import Org. dom4j. document; import Org. dom4j. extends entexception; import Org. dom4j. element; import Org. dom4j. io. saxreader; import COM. tgb. rssreader. bean. rssbean; import COM. tgb. rssreader. bean. rssconfigbean; import Com. tgb. rssreader. bean. rssteambean; /*** read the XML file * @ author longxuan **/public class readxml {// Private list of RSS group subscriptions <rssteambean> rssteambeanlist = new arraylist <rssteambean> (); /*** read RSS file list */Public void readrss () {// RSS file list Configuration Information Entity rssconfigmgr = new rssconfigmgr (); List <rssconfigbean> List = rssconfigmgr. getrssconfig (); string errtext = ""; // record error information // read the RSS file list cyclically for (rssconfigbean Rssconfig: List) {// system. out. println (rssconfig. getname () + "----" + // rssconfig. getpath (); try {// read the content of the RSS file readrss (system. getproperty ("user. dir ") + rssconfig. getpath ();} catch (exception e) {errtext + = E. getmessage () ;}}/ *** read the ompl file ** @ Param filepath */private void readrss (string filepath) {file = new file (filepath ); if (! File. exists () {// system. out. println ("[+ filepath +"] File not found "); // return; throw new runtimeexception (" ["+ filepath +"] File not found ");} try {// read and parse the XML document // saxreader is a pipeline. Read the XML file out in a stream mode: saxreader reader = new saxreader (); fileinputstream FCM = new fileinputstream (File); // The following is the document DOC = reader by parsing the XML string. read (FS); // get the root node element rootelt = Doc. getrootelement (); // get the root node // system. out. println ("root node :" + Rootelt. getname (); // get the root node name // get the head/Title node element titleelt = (element) rootelt. selectsinglenode ("head/Title"); // obtain the title of the subnode under the head node // obtain the group name String title = titleelt. gettexttrim (); // get the body node element bodyelt = (element) rootelt. selectsinglenode ("body"); // obtain the first outline node element outlineelt = (element) bodyelt under the body. selectsinglenode ("outline"); // determines the number of attributes of the outlineelt node.> 2 indicates the detailed blog subscription information. Otherwise, it indicates the group information. If (outlineelt. attributes (). size ()> 2) {// detailed blog subscription information // instantiate the RSS group entity rssteambean = new rssteambean (); // obtain the outline node iterator under the body node <?> Iter = bodyelt. elementiterator ("outline"); // output group name // system. out. println ("group name:" + title); // record the group name rssteambean. settitle (title); rssteambean. settext (title); // instantiate the subscription list <rssbean> rssbeanlist = new arraylist <rssbean> (); // obtain the detailed blog subscription information readblogsinfo (ITER, rssbeanlist ); // set the subscription list to rssteambean in the group. setrssbeanlist (rssbeanlist); // Add the group to the RSS group subscription list rssteambeanlist. add (rssteambean);} else {// group information // obtain outli under the body Node NE Node iterator <?> Iter = bodyelt. elementiterator ("outline"); While (ITER. hasnext () {// read all outline information under the outline node, each of which is a subscription record element teamelt = (element) ITER. next (); // instantiate the RSS group entity rssteambean = new rssteambean (); // re-obtain the group name Title = teamelt. attributevalue ("title"); string text = teamelt. attributevalue ("text"); // system. out. println ("group title:" + title + "text:" + // text); // record the group name and display name rssteambean. settitle (tit Le); rssteambean. settext (text); // instantiate the subscription list <rssbean> rssbeanlist = new arraylist <rssbean> (); // obtain the iterator of the outline node under the body node <?> Iterrss = teamelt. elementiterator ("outline"); // get the detailed blog subscription information readblogsinfo (iterrss, rssbeanlist); // set the subscription list to rssteambean in the group. setrssbeanlist (rssbeanlist); // Add the group to the RSS group subscription list rssteambeanlist. add (rssteambean) ;}} catch (filenotfoundexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (effecentexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}/*** read the current group's blog subscription information ** @ Pa Ram ITER * subnode iterator of the current node * @ Param rssbeanlist * subscribe list */private void readblogsinfo (iterator <?> ITER, list <rssbean> rssbeanlist) {// traverses all outline nodes. Each node is a subscription message while (ITER. hasnext () {rssbean = new rssbean (); element outlineelt = (element) ITER. next (); string htmlurl = outlineelt. attributevalue ("htmlurl"); // get the htmlurl attribute value of the current node string xmlurl = outlineelt. attributevalue ("xmlurl"); // get the xmlurl attribute value of the current node string version = outlineelt. attributevalue ("version"); // get the current node's version attribute value string type = outlineelt. attributevalue ("type"); // obtain the type attribute value of the current node string outlinetitle = outlineelt. attributevalue ("title"); // obtain the title attribute value of the current node string outlinetext = outlineelt. attributevalue ("text"); // get the text attribute value of the current node // system. out. print ("<outline htmlurl = \" "+ htmlurl +" \ ""); // system. out. print ("xmlurl = \" "+ xmlurl +" \ ""); // system. out. print ("version = \" "+ version +" \ ""); // system. out. print ("type = \" "+ Type +" \ ""); // system. out. print ("Title = \" "+ outlinetitle +" \ ""); // system. out. println ("text = \" "+ outlinetext +" \ "/>"); rssbean. sethtmlurl (htmlurl); rssbean. setxmlurl (xmlurl); rssbean. setversion (version); rssbean. settype (type); rssbean. settitle (outlinetitle); rssbean. settext (outlinetext); rssbean. settext (outlinetext); // store each subscription information to the rssbeanlist in the subscription list. add (rssbean) ;}}/*** get the RSS group subscription list ** @ return */public list <rssteambean> getrsstembeanlist () {return rssteambeanlist ;} public static void main (string [] ARGs) {readxml = new readxml (); readxml. readrss (); List <rssteambean> rsstembeanlist = readxml. getrsstembeanlist (); For (rssteambean: rsstembeanlist) {system. out. println ("[Group title:" + rssteambean. gettitle () + "text:" + rssteambean. gettext () + "]"); For (rssbean: rssteambean. getrssbeanlist () {system. out. print ("<outline htmlurl = \" "+ rssbean. gethtmlurl () + "\" "); // system. out. print ("xmlurl = \" "+ rssbean. getxmlurl () + "\" "); system. out. print ("version = \" "+ rssbean. getversion () + "\" "); system. out. print ("type = \" "+ rssbean. getType () + "\" "); system. out. print ("Title = \" "+ rssbean. gettitle () + "\" "); system. out. println ("text = \" "+ rssbean. gettext () + "\"/> ");} system. out. println ("-------------------------------------------------");}}}

Because JSP is not used for display, to view the effect, right-click the main function and choose "Run as"-"1 Java application" for testing. Here:



It can be fully read. The opml files exported from a single group of earlier versions and the new multi-group version can be read normally. In the following blog, how to read the result to JSP is displayed in the tree structure. In subsequent versions, add, delete, modify, and move the subscription information and copy the subscription information to the Group. It also provides the import and export functions. These will appear in subsequent blogs. Please do your best.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.