Java Write/Read XML

Source: Internet
Author: User

Package com.plas.util; Import Java.io.FileWriter; Import java.io.IOException; Import Java.util.HashMap; Import java.util.List; Import Java.util.Map; Import Org.jdom.Attribute; Import org.jdom.Document; Import org.jdom.Element; Import org.jdom.JDOMException; Import Org.jdom.input.SAXBuilder; Import Org.jdom.output.Format; Import Org.jdom.output.XMLOutputter; Import Com.plas.pojo.bean.SysAreacate; Import Com.plas.pojo.bean.SysNewsCate; Import Com.plas.pojo.bean.SysNewsSource; public class Analysisxml {//Specify XML parser, set default parser private Saxbuilder Builder; private document document; private Element roots ; Private String Xmlpath; /** * Initialization Information * @param xmlpath */Public Analysisxml (String xmlpath) {builder = new Saxbuilder (false); try {document = Buil Der.build (Xmlpath); catch (Jdomexception e) {//Todo auto-generated catch block E.printstacktrace ();} catch (IOException e) {/Todo Auto -generated Catch block E.printstacktrace (); }//get the XML document roots = Document.getrootelement ();//Get root element This.xmlpath = xmlpath; Public Analysisxml () {}/** * Delete * @param cateid * @return */public string Delete (String catecode) {Element ele =null; Deletexml (Roots.getchildren ("Cate"), Catecode); SaveXML (); return null; Public String Delete (Long catecode) {Element ele =null; Deletexml (Roots.getchildren ("Cate"), Catecode); SaveXML (); return null; /** * Recursive lookup XML * @param sublist * @param conint * @return * * Public Element recursivexml (List sublist,string catecode) {i F (Catecode.equals ("0")) return roots; if (sublist==null) return null; Element ele = null; for (int i=0;i<sublist.size (); i++) {ele = (Element) sublist.get (i); if (Ele.getattributevalue ("id"). Equals (Catecode ) {return ele;//returns the result set}else{Ele = Recursivexml (Ele.getchildren ("Cate"), Catecode); if (Ele!=null && Ele.getattributevalue ("id"). equals (Catecode)) return ele;//returns the result set} {null; /** * Recursive lookup XML * @param sublist * @param conint * @return * * Public Element recursivenewsxml (List sublist,long cateid) {i F (cateid==0) return roots;if (sublist==null) return null; Element ele = null; for (int i=0;i<sublist.size (); i++) {ele = (Element) sublist.get (i); if (Integer.parseint (Ele.getattributevalue ("id" ) ==cateid) {return ele;//returns the result set}else{Ele = Recursivenewsxml (Ele.getchildren ("Cate"), Cateid); if (Ele!=null && Integer.parseint (Ele.getattributevalue ("id")) ==cateid) return ele;//returning the result set}} returns null; /** * Recursive Lookup Delete * @param sublist * @param cateid * @return/public int deletexml (List sublist,string catecode) {Element E Le = null; for (int i=0;i<sublist.size (); i++) {ele = (Element) sublist.get (i); if (Catecode.equals (Ele.getattributevalue ("id") )) {sublist.remove (i);} else{Int J = deletexml (Ele.getchildren ("Cate"), Catecode); if (j!=0) Ele.getchildren ("Cate"). Remove (j);//return result set}} return 0; public int Deletexml (List sublist,long catecode) {Element ele = null; for (int i=0;i<sublist.size (); i++) {ele = (eleme NT) Sublist.get (i); if (Catecode==integer.parseint (Ele.getattributevalue ("id"))) {sublist.remove (i);} else{Int J = deletexml (Ele.getchildren ("Cate"), Catecode); if (j!=0) Ele.getchildren ("Cate"). Remove (j);//returns result set}} return 0; /** * Change XML content */public void SaveXML () {Format format = Format.getcompactformat (); Format.setencoding ("gb2312");//Set text This Code format.setindent (""),//Set line-wrapping indentation FileWriter writer; Xmloutputter output; try {writer = new FileWriter (xmlpath); output = new Xmloutputter (format); Output.output (document, writer); Writer.close () ; catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();}} /** * Administrative area settings generate the XML * */Public String getareacate (List list) {list Cate = null for the return page; StringBuffer buffer=new StringBuffer (); for (int i=0;i<list.size (); i++) {if ((sysareacate) list.get (i)). Getfatherid (). Equals ("0")) {cate= Roots.getchildren ("Cate"); } else{Cate=recursivexml (Roots.getchildren ("Cate"), ((sysareacate) list.get (i)). Getcatecode ()). GetChildren ("Cate" ); for (int j=0;cate!=null&&j<cate.size (); j + +) {element ele= (Element) Cate.get (j), buffer. Append (";cate><id> "). Append (Ele.getattributevalue (" id ")). Append (" </id> "). Append (" <name> "). Append ( Ele.getchild ("name"). GetText ()). Append ("</name>"). Append ("<down>"). Append (Ele.getchild ("Down"). GetText ()). Append ("</down>"). Append ("<catecode>"). Append (Ele.getchild ("Catecode"). GetText (). Append ("</catecode>"). Append ("</cate>"); } buffer.append ("</cates>"); return buffer.tostring (); Public String FindByID (long id) {return recursivenewsxml (Roots.getchildren ("Cate"), id). Getchild ("name"). GetText (); public string FindByID (string code) {return Recursivexml (Roots.getchildren ("Cate"), code). Getchild ("name"). GetText () ; Public String Findcode (long id) {return recursivenewsxml (Roots.getchildren ("Cate"), id). Getchild ("code"). GetText (); public string Findcode (string code) {return Recursivexml (Roots.getchildren ("Cate"), code). Getchild ("code"). GetText () ; Public String oneelement (Long ID) {//Element Ele = Recursivexml (roots.getchIldren ("Cate"), id); StringBuffer buffer = new StringBuffer ("<cates><cate>"); Buffer//. Append ("<name>")//. Append (Ele.getchild ("name"). GetText ())//. Append ("</name>")//. Append ( "<id>")//. Append (Ele.getchild ("id"). GetText ())//. Append ("</id>")//. Append ("<website>")//. Append (Ele.getchild ("website"). GetText ())//. Append ("</website>"); Buffer.append ("</cate></cates>"); Return "";} Public Document getdocument (String xmlpath) {builder = new Saxbuilder (false); try {Document = Builder.build (Xmlpath);} catch (Jdomexception e) {//Todo auto-generated catch block E.printstacktrace ();} catch (IOException e) {/Todo auto- Generated catch block E.printstacktrace (); }//gets the XML document return documents; /** * Find an element * @param ID * @return/Public element getoneelement (long id) {return recursivenewsxml (Roots.getchildren (" Cate "), id); Public Element getoneelement (String code) {return Recursivexml (Roots.getchildren ("Cate"), code); } }

When Java class calls:

Delete////////////////////public String deletexml () {List List=selectforfatherid ("0"); if (!list.isempty ()) { for (int i=0;i<list.size (); i++) {Analysisxml analysisxml = new Analysisxml (Servletactioncontext.getrequest (). Getrealpath ("/xmldata") + "//areacate.xml"); Analysisxml.delete (((sysareacate) list.get (i)). Getcatecode ()); } return null; ///////////Save/////////////////////Public String Save (list list) {sysareacate fathercate=null;//Analysisxml.delete ( "086"); Query parent Record if (List.isEmpty ()) {} else{for (int i=0;i<list.size (); i++) {fathercate= (sysareacate) list.get (i); Fathercate.setuptime (New Date ()); Fathercate.setdown (new Long (0)); Fathercate.setcatecode (Fathercate.getcatecode (). toLowerCase ()); Testareacate t = new testareacate (); Fathercate.setcntospell (T.getfullspell (Fathercate.getcatename ())); if (fathercate==null) fathercate.setdepth (new Long (1));//Level directory else{Fathercate.setdown (Fathercate.getdown () +1); Lower directory plus 1 fathercate.setdepth (fathercate.getdepth () +1); Sys.save (fAthercate); } analysisxml analysisxml = new Analysisxml (Servletactioncontext.getrequest (). Getrealpath ("/xmldata") + "// Areacate.xml "); Analysisxml.update (Fathercate.getcatecode (), Fathercate.getcatename (), Fathercate.getcatecode (), Fathercate.getfatherid (), 2); for (int i=0;i<list.size (); i++) {List Newlist=sysareacatedao.findbyfatherid ((sysareacate) list.get (i)). Getcatecode ()); Save (NewList); } return null; }

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.