DOM Document-driven
When we're working with the Dom , we need to read the entire XML Document, then create the DOM tree in memory , generate the Dom Each Node object on the tree
The advantage is that the XML document can be added to the complex operation of the search, you can always follow the relationship between nodes to access data
Disadvantages:
Unable to handle large documents due to limited memory capacity, low processing efficiency because of no indexing mechanism
DOM ( Document Object Model ), which defines a set of interfaces for parsing XML documents, the parser reads the entire document, and then constructs a tree structure that resides in memory, and the code can then use DOM interface to group the entire tree structure, the other points are as follows:
Advantages: The entire document tree is in memory, easy to operate, and supports the functions of deleting, modifying, rearranging and so on.
Cons: It's a waste of time and space to put an entire document into memory (often with a lot of useless nodes).
Use cases: Once the document is parsed, it needs to be accessed more than once, and the resources are plentiful (e.g. memory,CPU , etc.).
To solve these problems caused by DOM parsing XML , SAXappears. SAX parsing XML document is event driven
Package Com.huang;import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.transform.transformer;import Javax.xml.transform.transformerconfigurationexception;import Javax.xml.transform.transformerexception;import Javax.xml.transform.transformerfactory;import Javax.xml.transform.dom.domsource;import Javax.xml.transform.stream.streamresult;import org.w3c.dom.Document; Import org.w3c.dom.element;import org.w3c.dom.node;import org.w3c.dom.nodelist;//test Dom curd operation on XML file// Wrapping in XML and content in tags will output #textpublic class DOM {public static void main (string[] args) without judgment during traversal {// Gets an instance of an abstract class documentbuilderfactory dbf = Documentbuilderfactory.newinstance (); Try{documentbuilder db = Dbf.newdocumentbuilder ();D ocument Document = Db.parse ("./src/data.xml");//traverse the contents of the XML table list (document);// Query the name of the first student in the XML table//find (document);//Insert a student's information//add (document) in the XML table;//Modify the attribute and element value//update (document) in the XML table;// Delete a student or attribute//del (document) in the XML table; catch (Exception e) {e.printstacktrace ();}} TraverseAll the elements in the XML//and the contents of the tags in the XML are output #textpublic static void list (node node) {if (node.getnodetype () = = node if not judged during traversal. Element_node) {System.out.println (Node.getnodename ());} NodeList NodeList = Node.getchildnodes (); for (int t = 0; t < nodelist.getlength (); ++t) {Node n = nodelist.item (t); list (n);} return;} Delete a student in an XML table or property public static void Del (document document) throws Transformerexception{element node = (Element) document.getElementsByTagName ("Student"). Item (0);//Remove attribute Node.removeattribute ("id");//Remove elements element name = (Element) Node.getelementsbytagname ("name"). Item (0); Node.removechild (name);//Get Father node//node parentnode = Node.getparentnode () ;//parentnode.removechild (node);//write Transformerfactory TFF = Transformerfactory.newinstance () in the file XML; Transformer tf = Tff.newtransformer (); Tf.transform (new Domsource (document), New Streamresult ("Src/data.xml")); Modify a student's information in an XML table public static void update (document document) {Try{element Stuname = (Element) document.getElementsByTagName ("name"). Item (0); stuname.sEttextcontent ("Zhang"); Stuname.setattribute ("nickname", "Zhangsan");//write to file xml transformerfactory TFF = Transformerfactory.newinstance (); Transformer tf = Tff.newtransformer (); Tf.transform (new Domsource (document), New Streamresult ("Src/data.xml")); catch (Exception e) {e.printstacktrace ();}} Insert a student's information in the XML table public static void Add (document document) {Try{element Stu = document.createelement ("student");// Add attribute Stu.setattribute ("student title", "Tiger"); Element name = document.createelement ("name"); Name.settextcontent ("Ben"); Element StudentID = document.createelement ("StudentID"); Studentid.settextcontent ("123321"); Element sex = document.createelement ("Sex"); Sex.settextcontent ("male"); Stu.appendchild (name); Stu.appendchild ( StudentID) stu.appendchild (sex);//add Element Document.getdocumentelement () after the root node. appendchild (Stu);// Write to the file xml transformerfactory TFF = Transformerfactory.newinstance (); Transformer tf = Tff.newtransformer (); Tf.transform (new Domsource (document), New Streamresult ("Src/data.xml")); catch (Exception e) {e.printsTacktrace ();}} The name of the first student in the Query XML table is public static void find (document document) {NodeList NodeList = document.getElementsByTagName (" Student ");//type cast to subclass element type element Stu = (element) nodelist.item (0);//Get Property value System.out.println (" ID: "+ Stu.getattribute ("id")); NodeList name = Stu.getelementsbytagname ("name"); element FirstName = (Element) name.item (0);//Gets the element value System.out.println ("Name:" + firstname.gettextcontent ());}}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
DOM Parsing xml