1. Pojo class: Notice
Package Com.green.notice.storage;import Java.util.arraylist;import Java.util.list;public class Notice {private int id; private string Title;private string Content;private list<string>url=new arraylist<string> ();p rivate String Createtime;private string Uid;public void SetUid (string uid) {this.uid = uid;} Public String Getuid () {return UID;} public void setId (int id) {this.id = ID;} public int getId () {return ID;} public void Setcreatetime (String createtime) {this.createtime = Createtime;} Public String Getcreatetime () {return createtime;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Public String getcontent () {return content;} public void SetContent (String content) {this.content = content;} Public list<string> GetUrl () {return URL;} public void SetUrl (list<string> url) {this.url = URL;} @Overridepublic String toString () {return "Notice [title=" + title + ", content=" + content + ", url=" + url+ "]";}}
2. Parse class
Package Com.green.notice.storage;import Java.io.file;import Java.io.fileoutputstream;import java.io.IOException; Import Java.io.outputstreamwriter;import java.util.arraylist;import java.util.iterator;import java.util.List; Import Javax.swing.filechooser.fileview;import Org.dom4j.document;import Org.dom4j.documentexception;import Org.dom4j.documenthelper;import Org.dom4j.element;import Org.dom4j.io.outputformat;import Org.dom4j.io.SAXReader; Import Org.dom4j.io.xmlwriter;import org.xml.sax.saxexception;/* * @author: Maybo * @date: 2016-1-5 *????????????? */public class Noticexmlparse implements Iparsexml {private string path = "";p rivate string root = "notices";/* * @param: X ML??? ·?? */public noticexmlparse (String path) {this.path = path;} @Overridepublic synchronized void Add (Notice Notice) {try {file File=new file (path), if (!file.exists ()) Create (Root);D Ocument document = Documentfrompath (path); Element root = Document.getrootelement (); list<element> elements = root.elements (); Element element = documenthelper.createelement ("notice"); Element.addattribute ("id", elements.size () <=0?1+ "":( Integer.valueof (Elements.get (0). AttributeValue ("id", NULL)) +1) + ""); Element.addattribute ("title", Notice.gettitle ()); Element.addattribute ("Content", notice.getcontent ()); Element.addattribute ("Createtime", Notice.getcreatetime ()); for (String Url:notice.getUrl ()) {Element u = documenthelper.createelement ("url"); U.settext (URL); element.add (u);} Elements.add (0, Element); write (document);} catch (Saxexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (Documentexception e) {//Todo auto- Generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} @Overridepublic synchronized void Delete (int id) {try {document document = Documentfrompath (path); Element root = Document.getrootelement ();iterator<element> el = root.elementiterator ("notice"); while ( El.hasnext ()) {if (id + ""). Equals (El.next (). AttributeValue ("id", NULL))) {El.remove ();}} Write (document);} catch (Saxexception e) {//Todo auto-generated catch Blocke.printstacktrace ();} catch (Documentexception e) {//Todo auto- Generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} @Overridepublic List<notice> finds () throws Saxexception, documentexception {document document = Documentfrompath (path); Element root = Document.getrootelement (); list<element> elements = root.elements (); list<notice> notices = new arraylist<notice> (); for (Element e:elements) {Notice Notice = new Notice (); Notice . SetContent (E.attributevalue ("content", null)), Notice.setcreatetime (E.attributevalue ("Createtime", null)); Notice.setid (Integer.valueof (E.attributevalue ("id", null))); Notice.settitle (E.attributevalue ("title", null)); list<string> urls = new arraylist<string> (); list<element> Urlsel = e.elements ("url"), for (Element U:urlsel) {Urls.add (U.gettext ()); notice.SetUrl (URLs);} Notices.add (notice);} return notices;} @Overridepublic list<notice> finds (int index, int offset) throws Saxexception,documentexception {Document Document = Documentfrompath (path); Element root = Document.getrootelement (); list<element> elements = root.elements (), if (Offset < elements.size ()) {elements = elements.sublist (index, offset);} list<notice> notices = new arraylist<notice> (); for (Element e:elements) {Notice Notice = new Notice (); Notice . SetContent (E.attributevalue ("content", null)), Notice.setcreatetime (E.attributevalue ("Createtime", null)); Notice.setid (Integer.valueof (E.attributevalue ("id", null))); Notice.settitle (E.attributevalue ("title", null)); list<string> urls = new arraylist<string> (); list<element> Urlsel = e.elements ("url"), for (Element U:urlsel) {Urls.add (U.gettext ()); Notice.seturl (URLs);} Notices.add (notice);} return notices;} Private Document Documentfrompath (String path) throws Saxexception,documentexception {SaxreAder reader = new Saxreader ();D ocument document=null;if (new file (path). Exists ()) {Document = reader.read (new file (path)) ;} return document;} @Overridepublic void Create (String root) throws Documentexception, IOException {//?????????? OutputFormat format = Outputformat.createprettyprint ();//??? N??? Format.setencoding ("UTF-8"); XMLWriter writer = new XMLWriter (new OutputStreamWriter (New FileOutputStream (path), "UTF-8"), format);D Ocument document = Documenthelper.createdocument (); Element Elementroot = documenthelper.createelement (root);d ocument.setrootelement (elementroot); Writer.write ( document);//???? Д?? Writer.flush ();//?????? Writer.close ();} private void Write (document document) throws IOException {//?????????? OutputFormat format = Outputformat.createprettyprint ();//??? N??? Format.setencoding ("UTF-8"); XMLWriter writer = new XMLWriter (new OutputStreamWriter (New FileOutputStream (path), "UTF-8"), format); Writer.write (document);//???? Д?? Writer.flush ();//?????? Writer.closE ();} @Overridepublic int Total () throws Saxexception, Documentexception, ioexception {file File=new file (path); File.exists ()) Create (root);D ocument Document = documentfrompath (path); Element root = Document.getrootelement (); list<element> elements = root.elements (); return elements.size ();} @Overridepublic synchronized void Clearend () throws IOException, Saxexception, documentexception {document document = doc Umentfrompath (path); Element root = Document.getrootelement (); list<element> elements = root.elements (); Elements.remove (Elements.size ()-1); write (document);}}
Basic operation of XML files using DOM4J technology