JAXP's DOM summary

Source: Internet
Author: User

first, JAXP, Dom introduction


Jaxp:java API for XML programing

Dom:document Object method


DOM Features:

(1) All the XML documents are put into memory for parsing, so memory consumption is consumed;

(2) to be able to the XML to be added to the search;


<person>
	<name>xiazdong</name>
	<age>20</age>
</person>

To become a DOM tree:




Second, DOM API


Dom's commonly used 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) the operation through document;

(4) If you make changes to the XML, you need to transformerfactory create a transformer;

(5) preservation through transformer;


Common Operations
the Documentbuilderfactory classes are:

(1) Documentbuilderfactory factory = documentbuilderfactory. newinstance ();

(2) Documentbuilder builder = Factory.newdocumentbuilder ();
the Documentbuilder classes are:

(1) Document document = Builder.parse (new File (""))/parse XML and return to document node

(2) Document document = Builder.newdocument (); Create a document


The document classes are:


(1) NodeList list = getElementsByTagName ("name");//queue to get <name> label

(2) Element e = document.createelement ("name"); Create a <name> tag, and return


The NodeList classes are:

(1) Node node = Nodelist.item (i); To get the i+1 node of a node queue

(2) int length = Nodelist.getlength (); To get the length of a node queue


the node has:


(1) String text = Node.gettextcontent (); Get the value of a node

(2) Node.appendchild (node child); Add a child subnode to node tail

(3) Node.insertbefore (node Child,node beforenode);//Add a child subnode before the Beforenode child node of node

(4) NodeList list = Node.getchildnodes (); Get all child nodes of node

(5) Node parent = Node.getparentnode (); Get node's parent

(6) Node.removechild (node child); Remove child children nodes of node

(7) node.settextcontent (String text); Set node's value

(8) String name = Node.getnodename (); Get the name of the label
the element has:
In addition to the node class approach, there are

(1) Element.setattribute ("name", "value"); Set the properties of a label

(2) String value = Element.getattribute ("name"); Get the properties of a label

(3) Element.removeattribute ("name")//Remove Label properties


the tranformerfactory are:

(1) Transformerfactory tfactory = Transformerfactory.newinstance ();

(2) Transformer tf = Tfactory.newtransformer ();


the transformer are:

(1) tf.transform (Source source,result result); Output


the Domsource are:

(1) Domsource Source = new Domsource (document document);


the Streamresult are:

(1) Streamresult result = new Streamresult (New FileOutputStream ("1.xml"));


Iii. dom for crud
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", "a");
	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"));
}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.