JDOM operation XML Java + xml = JDOM! This is the goal of the JDOM designer. If you have used annoying sax or Dom to process XML, you will know why JDOM or jaxb is required. At this year's (2002) javaone conference, Jason Hunter, the main founder of JDOM, gave a wonderful speech about JDOM, entitled JDOM makes XML easy. Get and install JDOM at http://jdom.org/to download the latest jdomapp. Take the JDOM 1.0 2-step version as an example. After downloading and decompressing, The jdom jar file is the JDOM. jar file under the build Directory, which is added to the class path. In addition, JDOM also requires support for jar files such as xerces. jar and JAXP. jar in the lib directory. If the following error occurs during use:
Java. lang. nosuchmethoderror or Java. lang. noclassdeffounderror: ORG/XML/sax/saxnotrecognizedexception you need to ensure xerces. jar files are located in other XML classes such as JAXP or crimson in classpath. These class files, including older versions of xerces, may not support sax2.0 or DOM Level 2. As a result, the above error occurs. In a simple example, the JDOM processing method is similar to Dom, but it is mainly implemented using sax, so you don't have to worry about processing speed and memory. In addition, there are almost no interfaces in JDOM, and all the classes are real classes without class factory classes.
Below is the XML file used by the instance: myxml. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Library> <book title> JAVA Programming entry </book title> <author> Zhang San </author> <PRESS> e-publishing house </PRESS> <price> 35.0 </price> <publication date> </book> <title> application of XML in Java </title> <author> Li Si </author> <publisher> expected Publishing House </Publishing House> <price> 92.0 </price> <publication date> 2002-10-07 </publication date> </book> </library>
The following is the JSP file for the operation: Test. jsp
<% @ Page contenttype = "text/html; charset = GBK" %> <% @ page Language = "Java" Import = "Java. util. *, JDOM. test. * "%> <HTML>
The following is the JSP file submitted in the previous file: xmlation. jsp
<% @ Page contenttype = "text/html; charset = GBK" %> <% @ page Language = "Java" Import = "JDOM. test. * "%> <% IF (request. getparameter ("act ")! = NULL & request. getparameter ("act "). equals ("add") {xmlbean. addxml (request); out. println ("<p align = center> <br> added successfully <br> <a href = test. JSP> return </a> ");} else if (request. getparameter ("act ")! = NULL & request. getparameter ("act "). equals ("Del") {xmlbean. delxml (request); out. println ("<p align = center> <br> deletion successful <br> <a href = test. JSP> return </a> ");} else if (request. getparameter ("act ")! = NULL & request. getparameter ("act "). equals ("edit") {xmlbean. editxml (request); out. println ("<p align = center> <br> modification successful <br> <a href = test. JSP> return </a> ");} else {out. print ("<p align = center> <br> illegal operation <br> <a href = test. JSP> return </a> ") ;}%>
The following is the bean that operates the XML file: xmlbean. Java
Jsppackage JDOM. test;/*** XML read/write bean */import Java. io. *; import Java. util. *; import Org. JDOM. *; import Org. JDOM. output. *; import Org. JDOM. input. *; import javax. servlet. *; import javax. servlet. HTTP. *; public class xmlbean {private string bookname, author, pub, price, pubdate; Public String getbookname () {return bookname;} Public String getauthor () {return author ;} public String getpub () {return pub;} Public String getprice () {return price;} Public String getpubdate () {return pubdate;} public void setbookname (string bookname) {This. bookname = bookname;} public void setauthor (string author) {This. author = author;} public void setpub (string pub) {This. pub = pub;} public void setprice (string price) {This. price = price;} public void setpubdate (string pubdate) {This. pubdate = pubdate;} public xmlbean () {}/ *** read all information in the XML file */Public vector loadxml (string path) throws exception {vector xmlvector = NULL; fileinputstream Fi = NULL; try {Fi = new fileinputstream (PATH); xmlvector = new vector (); saxbuilder sb = new saxbuilder (); document DOC = sb. build (FI); element root = Doc. getrootelement (); // obtain the root element list books = root. getchildren (); // obtain the set of all child elements of the root element. Element book = NULL; xmlbean xml = NULL; For (INT I = 0; I <books. size (); I ++) {xml = new xmlbean (); book = (element) books. get (I); // get the element XML of the first book. setbookname (book. getchild ("name "). gettext (); XML. setauthor (book. getchild ("author "). gettext (); XML. setpub (book. getchild ("Press "). gettext (); XML. setprice (book. getchild ("price "). gettext (); XML. setpubdate (book. getchild ("publication date "). gettext (); xmlvector. add (XML) ;}} catch (exception e) {system. err. println (E + "error");} finally {try {fi. close ();} catch (exception e) {e. printstacktrace () ;}} return xmlvector;}/*** Delete specified information in the XML file */public static void delxml (httpservletrequest request) throws exception {fileinputstream Fi = NULL; fileoutputstream fo = NULL; try {request. setcharacterencoding ("GBK"); string Path = request. getparameter ("path"); int xmlid = integer. parseint (request. getparameter ("ID"); FI = new fileinputstream (PATH); saxbuilder sb = new saxbuilder (); document DOC = sb. build (FI); element root = Doc. getrootelement (); // obtain the root element list books = root. getchildren (); // get the set of all child elements of the root element books. remove (xmlid); // Delete the child element at the specified position // string indent = ""; // Boolean newlines = true; // xmloutputter outp = new xmloutputter (indent, newlines, "GBK"); format = format. getprettyformat (); format. setindent (""); format. setencoding ("UTF-8"); xmloutputter outp = new xmloutputter (format); fo = new fileoutputstream (PATH); outp. output (Doc, FO);} catch (exception e) {system. err. println (E + "error");} finally {try {fi. close (); FO. close ();} catch (exception e) {e. printstacktrace () ;}}/ *** Add the specified XML file information */public static void addxml (httpservletrequest request) throws exception {fileinputstream Fi = NULL; fileoutputstream fo = NULL; try {request. setcharacterencoding ("GBK"); string Path = request. getparameter ("path"); FI = new fileinputstream (PATH); saxbuilder sb = new saxbuilder (); document DOC = sb. build (FI); element root = Doc. getrootelement (); // obtain the root element list books = root. getchildren (); // obtain the set of all child elements of the root element string bookname = request. getparameter ("bookname"); string author = request. getparameter ("author"); string price = request. getparameter ("price"); string pub = request. getparameter ("pub"); string pubdate = request. getparameter ("pubdate"); // text newtext; element newbook = new element ("book"); element newname = new element ("book name"); newname. settext (bookname); newbook. addcontent (newname); element newauthor = new element ("author"); newauthor. settext (author); newbook. addcontent (newauthor); element newpub = new element ("Press"); newpub. settext (pub); newbook. addcontent (newpub); element newprice = new element ("price"); newprice. settext (price); newbook. addcontent (newprice); element newdate = new element ("Published Date"); newdate. settext (pubdate); newbook. addcontent (newdate); books. add (newbook); // Add sub-elements // string indent = ""; // Boolean newlines = true; // xmloutputter outp = new xmloutputter (indent, newlines, "GBK"); format = format. getprettyformat (); format. setindent (""); format. setencoding ("UTF-8"); xmloutputter outp = new xmloutputter (format); fo = new fileoutputstream (PATH); outp. output (Doc, FO);} catch (exception e) {system. err. println (E + "error");} finally {try {fi. close (); FO. close ();} catch (exception e) {e. printstacktrace () ;}}/ *** modify the specified information of the XML file */public static void editxml (httpservletrequest request) throws exception {fileinputstream Fi = NULL; fileoutputstream fo = NULL; try {request. setcharacterencoding ("GBK"); string Path = request. getparameter ("path"); int xmlid = integer. parseint (request. getparameter ("ID"); FI = new fileinputstream (PATH); saxbuilder sb = new saxbuilder (); document DOC = sb. build (FI); element root = Doc. getrootelement (); // obtain the root element list books = root. getchildren (); // obtain the set of all child elements of the root element, element book = (element) books. get (xmlid); string bookname = request. getparameter ("bookname"); string author = request. getparameter ("author"); string price = request. getparameter ("price"); string pub = request. getparameter ("pub"); string pubdate = request. getparameter ("pubdate"); // text newtext; element newname = book. getchild ("name"); newname. settext (bookname); // modify the name of the book to the new name. Element newauthor = book. getchild ("author"); newauthor. settext (author); element newpub = book. getchild ("Press"); newpub. settext (pub); element newprice = book. getchild ("price"); newprice. settext (price); element newdate = book. getchild ("publication date"); newdate. settext (pubdate); // books. set (xmlid, book); // modify the child element // string indent = ""; // Boolean newlines = true; // xmloutputter outp = new xmloutputter (indent, newlines, "GBK"); format = format. getprettyformat (); format. setindent (""); format. setencoding ("UTF-8"); xmloutputter outp = new xmloutputter (format); fo = new fileoutputstream (PATH); outp. output (Doc, FO);} catch (exception e) {system. err. println (E + "error");} finally {try {fi. close (); FO. close ();} catch (exception e) {e. printstacktrace ();}}}}