Introduction to XML operations in Java

Source: Internet
Author: User
Today, we have sorted out the java beans used to operate XML files in previous projects. A complete XML instance is compiled. In the whole instance, I used different XML operation methods for creating, parsing, and operating XML files. Import file: Import Java. io. *; import Java. util. *; import Org. w3C. dom. *; import javax. XML. parsers. *; import Org. JDOM. *; import Org. JDOM. output. *; import COM. newland. nlie. highway. pub_com.msgmgr.msg_bean; import javax. XML. transform. *; import javax. XML. transform. dom. domsource; import javax. XML. transform. stream. streamresult; create the XML file public Boolean create_xmlfile () {Org. JDOM. element root = new Org. JDOM. element ("msg_not I _info "); Org. JDOM. document Doc = new Org. JDOM. document (Root); // Add the child element: Org. JDOM. element name1 = new Org. JDOM. element ("dep_code"); name1.addattribute (New Org. JDOM. attribute ("value", "2222"); root. addcontent (name1); Org. JDOM. element name = new Org. JDOM. element ("noti_man"); Name. addattribute (New Org. JDOM. attribute ("user_code", "2222"); Org. JDOM. element plan_noti_time = new Org. JDOM. element ("plan_noti_time "); Plan_noti_time.addattribute (New Org. JDOM. attribute ("value", "10:10:10"); Org. JDOM. element msg_num = new Org. JDOM. element ("msg_num"); msg_num.addcontent ("message serial number"); plan_noti_time.addcontent (msg_num); Org. JDOM. element noti_way = new Org. JDOM. element ("noti_way"); noti_way.addcontent ("notification method"); plan_noti_time.addcontent (noti_way); Org. JDOM. element msg_date = new Org. JDOM. element ("msg_date"); msg_da Te. addcontent ("message date"); plan_noti_time.addcontent (msg_date); Org. JDOM. element msg_time = new Org. JDOM. element ("msg_time"); msg_time.addcontent ("message time"); plan_noti_time.addcontent (msg_time); Org. JDOM. element send_dep = new Org. JDOM. element ("send_dep"); send_dep.addcontent ("sending Department"); plan_noti_time.addcontent (send_dep); Org. JDOM. element send_man = new Org. JDOM. element ("send_man"); send_man.addcontent ("sender "); Plan_noti_time.addcontent (send_man); Org. JDOM. element msg_title = new Org. JDOM. element ("msg_title"); msg_title.addcontent ("Message Title"); plan_noti_time.addcontent (msg_title); Org. JDOM. element msg_cont = new Org. JDOM. element ("msg_cont"); msg_cont.addcontent ("message content"); plan_noti_time.addcontent (msg_cont); Org. JDOM. element attach = new Org. JDOM. element ("Attach"); attach. addcontent ("attachments"); plan_noti_time.addconten T (attach); Org. JDOM. element msg_type = new Org. JDOM. element ("msg_type"); msg_type.addcontent ("Message Type"); plan_noti_time.addcontent (msg_type); Org. JDOM. element noti_status = new Org. JDOM. element ("noti_status"); noti_status.addcontent ("Notification status"); plan_noti_time.addcontent (noti_status); Org. JDOM. element noti_pri = new Org. JDOM. element ("noti_pri"); noti_pri.addcontent ("notification priority"); plan_noti_time.addcontent (noti_pri); Name. addcontent (plan_noti_time); name1.addcontent (name); // use fileoutputstream to generate the XML text try {string ident = ""; Boolean isnew = true; string cset = "gb2312 "; xmloutputter outer = new xmloutputter ("", true, cset); outer. output (Doc, new fileoutputstream ("user_msg.xml"); Return true;} catch (ioexception e) {e. printstacktrace (); Return false ;}} generated XML file content: <? XML version = "1.0" encoding = "UTF-8"?> <Msg_noti_info> <dep_code value = "2222"> <noti_man user_code = "2222"> <plan_noti_time value = "10:10:10"> <msg_num> 2 </msg_num> <noti_way> notification method </noti_way> <msg_date> message date </msg_date> <msg_time> message time </msg_time> <send_dep> sender </send_dep> <send_man> </send_man> <msg_title> Message Title </msg_title> <msg_cont> message content </msg_cont> <attach> attachments </Attach> <msg_type> Message Type </msg_type> <noti_status> notification Status </noti_status> <noti_pri> notification priority </noti_pri> </plan_noti_time> </noti_man> </dep_code> </msg_noti_info>: public vector search_xml (string dep_code, string user_code) {vector searchvector = new vector (); msg_bean search_bean = new msg_bean (); try {file F = new file ("user_msg.xml "); documentbuilderfactory factory = documentbuilderfactory. newinstance (); documentbuilder builder = factory. newdocumentbuilder (); Org. w3C. dom. document Doc = builder. parse (f); integer int_temp; Org. w3C. dom. element root = Doc. getdocumentelement (); nodelist NL = root. getelementsbytagname ("dep_code"); For (INT I = 0; I <NL. getlength (); I ++) {If (dep_code.equals (root. getelementsbytagname ("dep_code "). item (I ). getattributes (). getnameditem ("value "). getnodevalue () = true) {Org. w3C. dom. element noti_man = (Org. w3C. dom. element) nL. item (I); nodelist nl1 = noti_man.getelementsbytagname ("noti_man"); For (Int J = 0; j <nl1.getlength (); j ++) {If (user_code.equals (noti_man.getelementsbytagname ("noti_man "). item (j ). getattributes (). getnameditem ("user_code "). getnodevalue () = true) {Org. w3C. dom. element plan_time = (Org. w3C. dom. element) nl1.item (j); nodelist nl2 = plan_time.getelementsbytagname ("plan_noti_time"); For (INT n = 0; n <nl2.getlength (); N ++) {If (plan_time.getelementsbytagname ("plan_noti_time "). item (n ). getattributes (). getnameditem ("value "). getnodevalue (). equals ("10:10:10") {Org. w3C. dom. element searchelement = (Org. w3C. dom. element) nl2.item (n); int_temp = integer. valueof (searchelement. getelementsbytagname ("msg_num "). item (n ). getfirstchild (). getnodevalue (); search_bean.setmsg_num (int_temp.intvalue (); search_bean.setnoti_way (searchelement. getelementsbytagname ("noti_way "). item (n ). getfirstchild (). getnodevalue (); search_bean.setmsg_date (searchelement. getelementsbytagname ("msg_date "). item (n ). getfirstchild (). getnodevalue (); search_bean.setmsg_time (searchelement. getelementsbytagname ("msg_time "). item (n ). getfirstchild (). getnodevalue (); search_bean.setsend_dep (searchelement. getelementsbytagname ("send_dep "). item (n ). getfirstchild (). getnodevalue (); search_bean.setsend_man (searchelement. getelementsbytagname ("send_man "). item (n ). getfirstchild (). getnodevalue (); search_bean.setmsg_title (searchelement. getelementsbytagname ("msg_title "). item (n ). getfirstchild (). getnodevalue (); search_bean.setmsg_cont (searchelement. getelementsbytagname ("msg_cont "). item (n ). getfirstchild (). getnodevalue (); search_bean.setattach (searchelement. getelementsbytagname ("Attach "). item (n ). getfirstchild (). getnodevalue (); search_bean.setmsg_type (searchelement. getelementsbytagname ("msg_type "). item (n ). getfirstchild (). getnodevalue (); search_bean.setnoti_status (searchelement. getelementsbytagname ("noti_status "). item (n ). getfirstchild (). getnodevalue (); search_bean.setnoti_pri (searchelement. getelementsbytagname ("noti_pri "). item (n ). getfirstchild (). getnodevalue (); searchvector. add (search_bean); plan_time.removechild (plan_time.getelementsbytagname ("plan_noti_time "). item (n )); // Delete the searched content }}}}}}////////******************* **************************************** ///////// Delete the key value and re-write the file transformerfactory tfactory = transformerfactory. newinstance (); transformer = tfactory. newtransformer (); domsource source = new domsource (DOC); streamresult result = new streamresult (New Java. io. file ("user_msg.xml"); transformer. transform (source, result ); //////////////////////////////////////// /// // return searchvector ;} catch (exception e) {e. printstacktrace (); return NULL ;}}
Related Article

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.