Dom Summary of JAXP

Source: Internet
Author: User
Document directory
  • Dom development steps:
1. Introduction to JAXP and Dom

JAXP: Java API for XML programing

DOM: Document Object Method

Dom features:

(1) Put all XML documents into memory for parsing, which consumes memory;

(2) addition, deletion, modification, and query of XML;

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

Change to DOM tree:

Ii. DOM API

Common Dom classes include documentbuilderfactory, documentbuilder, document, node, nodelist, element, attribute, transformerfactory, transformer, domsource, and streamresult;

Dom development steps:

(1) Obtain documentbuilder through documentbuilderfactory;

(2) Parse XML through documentbuilder and obtain document (the root node of the document );

(3) operate through document;

(4) If the XML is modified, transformerfactory is required to create a transformer;

(5) Save it through transformer;

Common Operations of the documentbuilderfactory class include:

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

(2) documentbuilder builder = factory. newdocumentbuilder ();

The documentbuilder class includes:

(1) document = builder. parse (new file (""); // Parse XML and return the document Node

(2) document = builder. newdocument ();
// Create a document

The document class includes:

(1) nodelist list = getelementsbytagname ("name"); // the queue for obtaining the <Name> tag

(2) Element E = Document. createelement ("name ");
// Create a <Name> label and return

The nodelist class includes:

(1) node = nodelist. item (I); // obtain the I + 1 node of the node queue

(2) int length = nodelist. getlength (); // obtain the node queue length.

Node:

(1) string text = node. gettextcontent (); // obtain the node Value

(2) node. appendchild (node child); // Add a child node to the end of the node

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

(4) nodelist list = node. getchildnodes ();
// Obtain all the subnodes of the node

(5) node parent = node. getparentnode (); // obtain the parent node of the node.

(6) node. removechild (node child); // remove the child node of the node

(7) node. settextcontent (string text); // set the node Value

(8) string name = node. getnodename (); // obtain the tag name.

Element: Besides the node class method

(1) element. setattribute ("name", "value ");
// Set tag attributes

(2) string value = element. getattribute ("name ");
// Obtain tag attributes

(3) element. removeattribute ("name") // remove tag attributes

Tranformerfactory contains:

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

(2) Transformer TF = tfactory. newtransformer ();

Transformer includes:

(1) TF. Transform (source, result );
// Output

Domsource includes:

(1) domsource source = new domsource (document );

Streamresult contains:

(1) streamresult result = new streamresult (New fileoutputstream ("1.xml "));

Iii. Dom crudxml:
<?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("20");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);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")));}

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.