first, JAXP, Dom introduction
Jaxp:java API for XML programing
Dom:document Object Method
DOM Features:
(1) The XML documents are all put into memory for parsing, thus consuming memory;
(2) to be able to increase the deletion of XML search;
<person>
<name>xiazdong</name>
<age>20</age>
</person>
Into the DOM tree:
Second, DOM API
Common DOM classes are: Documentbuilderfactory, Documentbuilder, Document, Node, NodeList, Element, Attribute, Transformerfactory, Transformer, Domsource, Streamresult and other categories;
DOM Development steps:
(1) obtaining documentbuilder through documentbuilderfactory;
(2) parsing XML through Documentbuilder, and obtaining document (root node of documents);
(3) to operate through document;
(4) If the XML is changed, you need to transformerfactory create a transformer;
(5) the preservation through transformer;
Common Operations
the Documentbuilderfactory class has:
(1) Documentbuilderfactory factory = documentbuilderfactory. newinstance ();
(2) Documentbuilder builder = Factory.newdocumentbuilder ();
the Documentbuilder class has:
(1) Document document = Builder.parse (new File (""));//parse XML and return document node
(2) Document document = Builder.newdocument (); Create a document
the document class has:
(1) NodeList list = getElementsByTagName ("name");//Get <name> label queue
(2) Element e = document.createelement ("name"); Create a <name> tag, and return
the NodeList class has:
(1) Node node = Nodelist.item (i); Obtaining the node i+1 node of a queue
(2) int length = Nodelist.getlength (); Get the length of the node queue
node has the following:
(1) String text = Node.gettextcontent (); Get the value of a node
(2) Node.appendchild (node child); Add a child node to the tail of node
(3) Node.insertbefore (node Child,node beforenode);//Add a child node before the Beforenode child node of node
(4) NodeList list = Node.getchildnodes (); Get node all child nodes
(5) Node parent = Node.getparentnode (); Get the parent node of node
(6) Node.removechild (node child); Remove the child node of node
(7) node.settextcontent (String text); Set the value of node
(8) String name = Node.getnodename (); Get the name of the label
the element is:
In addition to the node class method, there are
(1) Element.setattribute ("name", "value"); Set the properties of a label
(2) String value = Element.getattribute ("name"); Get the properties of a tag
(3) Element.removeattribute ("name")//Remove Tag Properties
the tranformerfactory include:
(1) Transformerfactory tfactory = Transformerfactory.newinstance ();
(2) Transformer tf = Tfactory.newtransformer ();
the transformer include:
(1) tf.transform (Source source,result Result); Output
the Domsource include:
(1) Domsource Source = new Domsource (document document);
the streamresult include:
(1) Streamresult result = new Streamresult (New FileOutputStream ("1.xml"));
third, Dom for CRUD
The XML is:
<?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<personlist>
<person>
< Name aaa= "xxxx" >xiazdong-1</name>
<age>20</age>
<salary>1000</salary>
</person>
<person>
<name>xiazdong-2</name>
<age>21</age>
<salary>2000</salary>
</person>
</personlist>
1.Create
private static void Create (Documentbuilder builder) throws Exception {
Document document = Builder.newdocument ();
element root = document.createelement ("person");
Element name = document.createelement ("name");
Name.settextcontent ("Xiazdong");
Element age = document.createelement ("Age");
Age.settextcontent ("a");
Root.appendchild (name);
Root.appendchild (age);
Document.appendchild (root);
Transformerfactory tfactory = Transformerfactory.newinstance ();
Transformer tf = Tfactory.newtransformer ();
Tf.transform (new Domsource (document), New Streamresult (New FileOutputStream ("Output.xml"));
}
private static void Insert (document document) throws Exception {
Node person = document.getelementsbytagname (' Person "). Item (0);
Node name = document.getElementsByTagName ("name"). Item (0);
Element tmp = document.createelement ("Tmpchild");
Tmp.settextcontent ("xxx");
Person.insertbefore (tmp, name);
Transformerfactory tfactory = Transformerfactory.newinstance ();
Transformer tf = Tfactory.newtransformer ();
Tf.transform (new Domsource (document), New Streamresult (New FileOutputStream ("1.xml"));
}
2.Read
private static void Read (document document) {
node node = document.getelementsbytagname ("name"). Item (0);
String name = Node.getnodename ();
String value = Node.gettextcontent ();
String attr = (Element) node. getattribute ("AAA");
SYSTEM.OUT.PRINTLN (name);
System.out.println (value);
System.out.println (attr);
}
Application: Traverse the entire XML document and output
private static void ReadAll (document document) {
Node root = document.getElementsByTagName ("Personlist"). Item (0); C1/>read (root);
}
private static void Read (node node) {
if (node instanceof Element)
System.out.print ("<" +node.getnodename () + "></" +node.getnodename () + ">");
NodeList list = Node.getchildnodes ();
for (int i=0;i<list.getlength (); i++) {
Node n = list.item (i);
Read (n);
}
}
3.Update
private static void Update (document document) throws Exception {
node node = document.getelementsbytagname ("name"). Item (0);
Node.settextcontent ("Xiazdong");
Element e = (element) node;
E.setattribute ("AAA", "111");
Transformerfactory tfactory = Transformerfactory.newinstance ();
Transformer tf = Tfactory.newtransformer ();
Tf.transform (new Domsource (document), New Streamresult (New FileOutputStream ("1.xml"));
}
4.Delete
private static void Delete (document document) throws exception{
Node name = document.getElementsByTagName ("name"). Item (0);
Name.getparentnode (). removechild (name);
Transformerfactory tfactory = Transformerfactory.newinstance ();
Transformer tf = Tfactory.newtransformer ();
Tf.transform (new Domsource (document), New Streamresult (New FileOutputStream ("1.xml"));
}