DOM parsing XML
DOM document-driven
When processing the DOM, We need to read the entire XML document, and then create a DOM tree in the memory to generate each Node object on the DOM tree.
Advantage: You can add, delete, modify, and query XML documents, and access data at any time based on the relationship between nodes.
Disadvantages:
Unable to process large documents due to limited memory capacity. Because there is no indexing mechanism, the processing efficiency is low.
DOM (Document Object Model) defines a set of interfaces for parsing XML documents. The parser reads the entire document and constructs a tree structure with resident memory, then the code can use the DOM interface to operate the entire tree structure of the group. The other points are as follows:
Advantage: the entire document tree is in the memory for easy operation. It supports deletion, modification, and re-arrangement among other functions.
Disadvantage: transferring the entire document to memory (often containing a large number of useless nodes) wastes time and space.
Usage: Once the file is parsed, you need to access the data multiple times and have sufficient resources (such as memory and CPU ).
To solve these problems caused by DOM parsing XML, the following occurs. The XML document for parsing by SAX 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 the dom curd operation on the XML file. // If the newline and TAG content in the XML file are not judged during the traversal process, the output is # textpublic class DOM {public static void main (String [] args) {// obtain the instance DocumentBuilderFactory dbf = DocumentBuilderFactory of the abstract class. newInstance (); try {DocumentBuilder db = dbf. newDocumentBuilder (); Document document = db. parse (. /src/data. xml); // traverse the content list (document) in the XML table; // query the name of the first Student in the XML table // find (document ); // insert a student's information into the XML table // add (document); // modify attributes and element values in the XML table // update (document ); // delete a student or attribute in the XML table // del (document);} catch (Exception e) {e. printStackTrace ();}} // traverse all elements in the xml. // if the elements in the line feed and the content in the tag in the XML are not judged during the traversal process, the output is # textpublic static void list (Node node) {if (node. getNodeType () = node. 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 or attribute public static void del (Document document) in the XML table) throws TransformerException {Element node = (Element) document. getElementsByTagName (student ). item (0); // remove the node property. removeAttribute (id); // removes the Element name = (Element) node. getElementsByTagName (name ). item (0); node. removeChild (name); // obtain the parent Node // node parentnode = Node. getParentNode (); // parentnode. removeChild (node); // write TransformerFactory tff = TransformerFactory in the XML file. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (src/data. xml);} // modify the information of a student in the XML table public static void update (Document document) {try {Element stuName = (Element) document. getElementsByTagName (name ). item (0); stuName. setTextContent (zhang); stuName. setAttribute (Small name, zhangsan); // write 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 the property 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 the element document behind the root node. getDocumentElement (). appendChild (stu); // write TransformerFactory tff = TransformerFactory in the XML file. newInstance (); Transformer tf = tff. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (src/data. xml);} catch (Exception e) {e. printStackTrace () ;}// query the name of the first student in the XML table, public static void find (Document document) {NodeList nodelist = document. getElementsByTagName (student); // type forced conversion to subclass Element type Element stu = (Element) nodelist. item (0); // obtain the attribute value System. out. println (id: + stu. getAttribute (id); NodeList name = stu. getElementsByTagName (name); Element firstname = (Element) name. item (0); // obtain the element value System. out. println (name: + firstname. getTextContent ());}}
Running result: