Packagecom. Heibeiedu.test2;ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importorg.junit.Test;Importorg.w3c.dom.*;Importorg.xml.sax.SAXException;Importjavax.xml.parsers.*;Importjavax.xml.transform.*;ImportJavax.xml.transform.dom.DOMSource;Importjavax.xml.transform.stream.*;Importjavax.xml.xpath.*; Public classparsexml{ Public Static voidMain (string[] args) {Documentbuilderfactory factory=documentbuilderfactory.newinstance (); Element Thebook=NULL, theelem=NULL, root=NULL; Try{factory.setignoringelementcontentwhitespace (true); Documentbuilder DB=Factory.newdocumentbuilder (); Document xmldoc=db.parse (NewFile ("Myxml.xml")); Root=xmldoc.getdocumentelement (); //---Start a new book----Thebook=xmldoc.createelement ("book"); Theelem=xmldoc.createelement ("name"); Theelem.settextcontent (Book); Thebook.appendchild (Theelem); Theelem=xmldoc.createelement ("Price"); Theelem.settextcontent ("20"); Thebook.appendchild (Theelem); Theelem=xmldoc.createelement ("Memo"); Theelem.settextcontent ("The new book looks better." "); Thebook.appendchild (Theelem); Root.appendchild (Thebook); System.out.println ("---Start a new book----"); Output (xmldoc); //---Create a new book to complete----//---The following to make some changes to the "Harry Potter". ---- //---Search for "Harry Potter"----thebook= (Element) selectSingleNode ("/books/book[name= ' Harry Potter ']", Root); System.out.println ("---Search for Harry Potter----"); Output (Thebook); //---Revise the price of the book at this time-----Thebook.getelementsbytagname ("Price"). Item (0). Settextcontent ("15");//getElementsByTagName returns the nodelist, so keep the item (0). In addition, getElementsByTagName ("price") is equivalent to the XPath ".//Price ". SYSTEM.OUT.PRINTLN ("---Revise the price of this book at this time----"); Output (Thebook); //---also want to add an attribute ID with a value of B01----Thebook.setattribute ("id", "B01"); System.out.println ("---also want to add a property ID with a value of B01----"); Output (Thebook); //---to the completion of the revision of Harry Potter. ---- //---Want to delete the book "Kingdoms" with the id attribute----thebook= (Element) selectSingleNode ("/books/book[@id = ' B02 ']", Root); System.out.println ("---To delete the Kingdoms book with the id attribute----"); Output (Thebook); Thebook.getparentnode (). removechild (Thebook); System.out.println ("---the deleted XML----"); Output (xmldoc); //---Delete all books with a price below 10----NodeList somebooks=selectnodes ("/books/book[price<10]", Root); System.out.println ("---Delete all books with a price below 10---"); System.out.println ("---eligible books have" +somebooks.getlength () + ". ---"); for(intI=0;i<somebooks.getlength (); i++) {Somebooks.item (i). Getparentnode (). RemoveChild (Somebooks.item (i)); } output (xmldoc); SaveXML ("Test1_edited.xml", xmldoc); } Catch(parserconfigurationexception e) {e.printstacktrace (); } Catch(saxexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } Public Static voidOutput (node node) {//output node's XML string to the consoleTransformerfactory transfactory=transformerfactory.newinstance (); Try{Transformer Transformer=Transfactory.newtransformer (); Transformer.setoutputproperty ("Encoding", "Utf-8"); Transformer.setoutputproperty ("Indent", "yes"); Domsource Source=NewDomsource (); Source.setnode (node); Streamresult result=NewStreamresult (); Result.setoutputstream (System.out); Transformer.transform (source, result); } Catch(transformerconfigurationexception e) {e.printstacktrace (); } Catch(transformerexception e) {e.printstacktrace (); } } Public StaticNode selectSingleNode (String Express, Object source) {//finds a node and returns the first qualifying nodeNode result=NULL; Xpathfactory xpathfactory=xpathfactory.newinstance (); XPath XPath=Xpathfactory.newxpath (); Try{result=(Node) xpath.evaluate (Express, Source, Xpathconstants.node); } Catch(xpathexpressionexception e) {e.printstacktrace (); } returnresult; } Public StaticNodeList selectnodes (String Express, Object source) {//finds nodes that return a set of nodes that meet the criteria. NodeList result=NULL; Xpathfactory xpathfactory=xpathfactory.newinstance (); XPath XPath=Xpathfactory.newxpath (); Try{result=(NodeList) xpath.evaluate (Express, Source, Xpathconstants.nodeset); } Catch(xpathexpressionexception e) {e.printstacktrace (); } returnresult; } Public Static voidSaveXML (String fileName, Document doc) {//output document to a fileTransformerfactory transfactory=transformerfactory.newinstance (); Try{Transformer Transformer=Transfactory.newtransformer (); Transformer.setoutputproperty ("Indent", "yes"); Domsource Source=NewDomsource (); Source.setnode (DOC); Streamresult result=NewStreamresult (); Result.setoutputstream (NewFileOutputStream (fileName)); Transformer.transform (source, result); } Catch(transformerconfigurationexception e) {e.printstacktrace (); } Catch(transformerexception e) {e.printstacktrace (); } Catch(FileNotFoundException e) {e.printstacktrace (); }} @Test Public voidTest ()throwsexception{File xmlfile=NewFile ("Selectsetupinfo/selectsetup.xml"); Documentbuilderfactory builderfactory=documentbuilderfactory.newinstance (); Documentbuilder Builder=Builderfactory.newdocumentbuilder (); Document Doc=Builder.parse (xmlfile); Doc.getdocumentelement (). normalize (); NodeList nList= Doc.getelementsbytagname ("Selectsetup"); Node Node= Nlist.item (0); Element Ele=(Element) node; Ele.getelementsbytagname ("Interva"). Item (0). Settextcontent ("222"); Ele.getelementsbytagname ("Selecttime"). Item (0). Settextcontent ("44:44"); String Interva= Ele.getelementsbytagname ("Interva"). Item (0). Gettextcontent (); String Selecttime= Ele.getelementsbytagname ("Selecttime"). Item (0). Gettextcontent (); SaveXML ("Selectsetupinfo/selectsetup.xml", Doc); }}
Java implementation of XML file additions and deletions to search