DOM Basic & Example Introduced the basic method of SAX in the previous chapter, for SAX himself with serial characteristics, so read and write more convenient but, the DOCUMENT OBJECT MODEL, which will be described next. It is also a method of processing XML, but his use method is completely different from SAX, and his processing method is a bit similar to the tree in the data structure, and it is similar to the tree in processing. When he deals XML, he reads the entire XML file into the memory, then build a tree like us as seen in browsing, through the operation of the tree, we can complete the delete addition, etc. When the tree is mentioned, you must first introduce several objects used, through these objects, better completion of the DOM pairs of XML. A basic structure of the tree in the memory is first given before introducing these data types.
Document ----------- Document Type
| _______ Element ----------- ELEMENT
| ____ attr
| ____ text
The above is just a simple figure. In this figure, ELEMENT is nested, and the tree species also contains entity annotations. During the processing of the DOM, it is similar to each node in the above tree, which is an Node object. This Node object can be seen as the node of the tree when we learn the data structure, and you can get its child node, the parent node. Since XML also has attributes, both the attributes and child nodes of XML can make multiple, so DOM also provides two class nodelist, and NameDNodeMap can easily handle multiple nodes. GetChilds () through Node can get a NodeList containing all child nodes containing Node, through Node's getAttributes () method, you can get all properties of the node, the return value of this method is NamedNodeMap through the returned value through this return value. You can get all attribute values.
How to deal with these nodes of the DOM tree through these How can I know what type, element or an entity, or which type? This is the problem to be processed next. Each node taken from the number of DOM has a corresponding type, which can be obtained through the getNodeType () method in Node, and there is a type of all elements in XML in the Node interface, as long as it is NodeType with the node you get. Comparison with known types can know what type, different types of different types. Generally, all nodes in the DOM tree are used in a Switch () ... CASE statement.
If you want to complete the addition of deletion modification to the DOM tree, you need a certain data structure knowledge, which is not described in detail, the processing method is the same as the tree, which is just a tree, not a binary tree. If it is easy to modify the data, it is more troublesome to modify the structure. How can I write it back to the file after the DOM tree modification is completed, I know that the tree is not serial, but the written file is serial. So this requires a method of traversal. However, the method of this traversal and the establishment of tree structure are related to how to be Document is in the root, so the method of traversing the trees is also recursive. The processing process is like this:
1, process the Document node.
2, handle his child node with the same method.
3. Each node is used to handle their child nodes in the same way to form recursive. Know the entire tree.
Here is a specific example, just this example, I can't write data or read data or forget the master to advise, thank you first.
Import org.w3c.dom. *;
Import org.w3c.dom.document; import Org.apache.xerces.Impl.xs.dom.Dompival;
Import java.io. *;
Import org.xml.sax. *;
PUBLIC CLASS DOUTPUT {
Private DompARSER PARSER = New DOMPARSER ();
PRIVATE DOCUMENT DOC;
Public domoutput () {
}
Private void init ()
{
Try {
Parser.Parse ("first1.xml");
Doc = parse.getdocument ();
FileWriter FW = New FileWriter ("Second.xml");
Serialize (DOC, FW);
Fw.close ();
}
Catch (IOException EX) {
EX.PrintStackTrace ();
}
Catch (SAXEXCEPTION EX) {
EX.PrintStackTrace ();
}
}
Public void Serialize (Document Docu, Writer Writer)
{
Serialize (Docu, Writer, ");
Try {
Writer.flush ();
}
Catch (IOException EX) {
EX.PrintStackTrace ();
}
}
Private Void Serialize (Node Node, Writer Writer, String Indenter)
{
Try
{
Switch (node.getnodetype ()) {
Case Node.Document_Node:
Writer.write (" xml version = /"1.0/"?n");
Document DOC1 = (Document) Node;
Serialize (DOC1.GetDocumentElement (), Writer, "");
Break;
Case node.element_node:
String name = node.getnodename ();
String names = name;
String value = ""
NamedNodemap nnm = node.getattributes ();
For (int I = 0; i { Name = "" nnm.item (i) .GetnodeName () "= /" " nnm.Item (i) .GetnodeValue () " / " } Writer.write ("<" Name ">"); Nodelist childs = node.getchildnodes (); IF (Childs! = null && childs.getlength ()> 0 && childs.Item (0) .Getnodetype () == node.ement_node) { For (int i = 0; i Serialize (Childs.Item (i), Writer, ""); } Else { Value = node.getnodevalue (); IF (value == null) Value = ""; } Writer.write (Value "" Names "> / n"); Break; Case Node.Text_Node: Writer.write (Node.GetnodeValue ()); System.out.println (Node.GetnodeValue ()); Break; DEFAULT: Break; } } catch (ioException ex) { EX.PrintStackTrace (); } } Public static void main (String [] args) { Domoutput Domoutput = new domoutput (); Domoutput.init (); } } The XML files used in this example and the same here are no longer posted, and the number of hearts to build with DOM is mainly used to add new nodes. A simple example is also given below, but this example is to use the ship line in the above example, so it is best to put them in the same directory. Package testdom; Import Org.w3c.dom.domplementation; Import org.apache.xerces.dom.domImmementationImpl; Import org.w3c.dom.document; Import org.w3c.dom. *; Import java.io.filewriter; Import java.io. *; Public class createdom { Public createdom () { } Private void init () { Domillmentation DomiMPL = New DomIMplementationImpl (); Document doc = domimpl.createdocument (NULL, ITEM ", NULL; ELEMENT ELE = Doc.getDocumentElement (); Element item1 = doc.createElement ("name"); Item1.SetaTRibute ("ID", "001"); Text item1tetext = doc.createtextNode ("this is a test"); Item1.Appendchild (item1tetext); Ele.Appendchild (item1); Element item2 = doc.createElement ("name"); Item2.setttribute ("ID", "002"); Item2.setnodeValue ("Pencil"); Text item2text = doc.createtextNode ("TEST2"); item2.appendchild (item2text); Ele.AppendChild (item2); Domoutput out = new domoutput (); Try { Out.Serialize (DOC, New FileWriter ("third.xml"); } Catch (IOException EX) { EX.PrintStackTrace (); } } Public static void main (string [] args) {createdom Dom = new createdom (); Dom.init (); } } In the creation process, I have encountered the same problem is that I can't write data. If a friend can help me get this problem, the younger brother is not grateful, just learn XML, it is ugly, but this is also a little experience in the learning process. Written it hopes to help others, but also to make yourself can deepen understand. The DOM only introduces his relatively simple method, it is set to start, and some advanced knowledge of DOM is given in the subsequent chapter.