This is an example of XML operations using Java W3C Dom, including basic operations for querying, adding, modifying, deleting, and saving. A complete description of the entire XML operation process. It is suitable for users who are new to Java XML operations for reference and learning.
Suppose there is an XML file: test1.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Books>
<Book>
<Name> Harry Potter </Name>
<Price> 10 </price>
<Memo> This is a nice book. </Memo>
</Book>
<Book id = "B02">
<Name> Romance of the Three Kingdoms </Name>
<Price> 10 </price>
<Memo> one of the four famous books. </Memo>
</Book>
<Book id = "b03">
<Name> region </Name>
<Price> 6 </price>
<Memo> one of the four famous books. </Memo>
</Book>
<Book id = "b04">
<Name> redhouse </Name>
<Price> 5 </price>
<Memo> one of the four famous books. </Memo>
</Book>
</Books>
Below is test. Java
Import Java. io. file; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Org. w3C. dom. *; import Org. XML. sax. saxexception; import javax. XML. parsers. *; import javax. XML. transform. *; import javax. XML. transform. dom. domsource; import javax. XML. transform. stream. *; import javax. XML. XPath. *; public class test {public static void main (string [] ARGs) {documentbuild Erfactory factory = documentbuilderfactory. newinstance (); element thebook = NULL, theelem = NULL, root = NULL; try {factory. setignoringelementcontentwhitespace (true); documentbuilder DB = factory. newdocumentbuilder (); document xmldoc = dB. parse (new file ("test1.xml"); root = xmldoc. getdocumentelement (); // --- start a new book ---- thebook = xmldoc. createelement ("book"); theelem = xmldoc. createelement ("name"); theelem. sette Xtcontent ("new 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 ("--- create a new book ----"); output (xmldoc); // --- create a new book ---- // --- make some modifications to Harry Potter. ---- // --- Query for Harry Potter ---- thebook = (element) selectsinglenode ("/books/book [name = 'Harry Potter ']", root); system. out. println ("--- query for Harry Potter ----"); output (thebook); // --- modify the price of this book ----- thebook. getelementsbytagname ("price "). item (0 ). settextcontent ("15"); // getelementsbytagname returns nodelist, so keep up with item (0 ). In addition, getelementsbytagname ("price") is equivalent to ". // price" of xpath ". System. out. println ("--- modify the price of this book at this time ----"); output (thebook); // --- you also want to add an attribute ID with the value B01 ---- thebook. setattribute ("ID", "B01"); system. out. println ("--- you also want to add an attribute ID with the value B01 ----"); output (thebook); // --- modify Harry Potter. ---- // --- Use the ID attribute to delete the book "Romance of the Three Kingdoms" ---- thebook = (element) selectsinglenode ("/books/book [@ ID = 'b02']", root ); system. out. println ("--- use the ID attribute to delete the book" Romance of the Three Kingdoms "); output (thebook); thebook. getparentnode (). removechild (thebook); system. out. println ("--- XML after deletion ----"); output (xmldoc ); // --- delete all books whose prices are lower than 10 ---- nodelist somebooks = selectnodes ("/books/book [price <10]", root); system. out. println ("--- delete all books with prices lower than 10- -- "); System. Out. println (" --- eligible books include "+ somebooks. getlength () +. --- "); For (INT I = 0; I <somebooks. getlength (); I ++) {somebooks. item (I ). getparentnode (). removechild (somebooks. item (I);} output (xmldoc); savexml ("test10000edited.xml", xmldoc);} catch (parserconfigurationexception e) {e. printstacktrace ();} catch (saxexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}} public static void output (node) {// output the node's XML string to the console transfo Rmerfactory transfactory = transformerfactory. newinstance (); try {transformer = transfactory. newtransformer (); transformer. setoutputproperty ("encoding", "gb2312"); transformer. setoutputproperty ("indent", "yes"); domsource source = new domsource (); source. setnode (node); streamresult result = new streamresult (); result. setoutputstream (system. out); transformer. transform (source, result);} catc H (transformerconfigurationexception e) {e. printstacktrace ();} catch (transformerexception e) {e. printstacktrace () ;}} public static node selectsinglenode (string Express, object source) {// find the node and return the first node that meets the condition, node result = NULL; xpathfactory = xpathfactory. newinstance (); XPath = xpathfactory. newxpath (); try {result = (node) XPath. evaluate (Express, source, xpathconstants. node);} catc H (xpathexpressionexception e) {e. printstacktrace ();} return result;} public static nodelist selectnodes (string Express, object source) {// find a node and return a node set that meets the conditions. Nodelist result = NULL; xpathfactory = xpathfactory. newinstance (); XPath = xpathfactory. newxpath (); try {result = (nodelist) XPath. evaluate (Express, source, xpathconstants. nodeset);} catch (xpathexpressionexception e) {e. printstacktrace ();} return result;} public static void savexml (string filename, document DOC ){}}