Analytic method of document tree

Source: Internet
Author: User

The XML text used in this summary

1, <?xml version= "1.0" encoding= "UTF-8" standalone= "no"?>
<!--introduction of a DTD file tagged--><!--<! DOCTYPE Bookshelf SYSTEM "BOOK.DTD" >-
< bookshelf >
< book isbn= "a" press = "Publishing House" >
< title > Kingdoms </title >
< author > Luo guanzhong </author >
< wholesale price >500¥</wholesale price >
< price >100$</price >
</book >
< book isbn= "B" press = "Publishing House" >
< title > Journey to </title >
< author > Wu Chengen </author >
< price >55¥</price >
</book >
</Bookshelf >
Second, using JAXP to carry on the DOM analytic method
1, first to obtain the Analytical factorydocumentbuilderfactory
Javax.xml.parsers class

**documentbuilderfactory: Defines a factory API that enables applications to get parsers that generate DOM objects from an XML document.

Documentbuilderfactory factory = documentbuilderfactory.newinstance ();

**newinstance () Method: Gets the new real class of the documentbuilderfactory. This static method creates a new factory real class.
2. Get the parserDocumentbuilder
Class Documentbuilder
* * Define the API to make it from the XML document to the DOM document Real class. Application programmers can get a document from XML

Documentbuilder builder = Factory.Newdocumentbuilder ();

**newdocumentbuilder: Creates a new Documentbuilder real class with the parameters of the current configuration.

3. Parse the developed XML document. Get the Document object for the DOM tree that represents memory

DocumentDocument = Builder.Parse ("Src/book.xml");
**builder.parse () method: Resolves the contents of a given input source to an XML document. and returns a new DOM document object

Third, get the content of a specific node: columns such as: Print the author of the second book
1, according to the contents of the label to obtain all the author's name
NodeList nodelist=document.getElementsByTagName ()
NodeList: interface provides an abstraction of an ordered set of nodes
2. Get the second author element by index
Node node=nodelist.Item (1);
3. Print the text of the element
String Text=node.gettextcontent ();
System.out.println (text);
Iv. traversing all ELEMENT nodes: the node name of the printed element
1. Determine if the current node is a node of an element
Short Nd=node.getnodetype ();
if (Nd==node.element_node) {
If it is: print the name of the element node
System.out.println (Node.getnodename ());
}
If not to find their child node
NodeList nodelist=node.getchildnodes ();
int length=nodelist.getlength ();//Gets the length of the Child's node
for (int 1=0;i<length;i++) {
Traverse child nodes for recursion
Node N=nodelist.item (i);
Test2 (n);
}
V. Modifying the topic content of ELEMENT nodes
1, column: Find the price of the first book
NodeList NodeList = docuement.getelementsbytagname ("price");
2. Set the content of the theme
Node No=nodelist.item (0);//Get his nodes
No.settextcontent ("100$");//Set his subject content
3.write the document tree in memory back into the XML
Transformerfactory factory = Transformertactory.newinstance ();
Transformer tr = Factory.newtransformer ();
Tr.transformer (New Domsource (doument), New Streamresult ("Src/book.xml"));
Vi. adding child element nodes to the specified element node
1. Create a new element and set its contents
Element element = element.createelement ("Internal price");
Set his text content
Element.settexstcontent ("300$");
2. Find the first book
NodeList NodeList = document.getElementsByTagName ("book");
Node first =nodelist.item (0);
3. Attach the new node to the first book
First.appendchild (Element)
4. Write the document tree in memory into XML
Transformerfactory factory = Transformerfactory.newinstace ();
Transformer Tr=factory.newtransformer ();
Tr.transformer (new Domsource (document), New Streamresult ("Src/book.xml"));
Add a sibling element node to the specified element node: Columns such as: Add a wholesale price before the price of the first book
1. Create and set up the theme content
Element element = documcnet.createelement ("wholesale price");
Element.settextcontent ("500$");
2. Find the price of the first book
NodeList NodeList = document.getElementsByTagName ("price");
Node node=nodelist.item (0);
3. Add the newly created element in front of the selling price:
To add a child node, be sure to use the parent element to
Node.getparentnode (). insertbefore (Element,node);
4. Write the document tree in memory back into XML
Transformerfactory factory = Transformerfactory.newinstance ();
Transformer tr = Factory.newtransformer ()
Tr.transformer (new Domsource (document), New Streamresult ("Src/book.xml"));
Viii. Delete the node of the specified element
1. Deleting child nodes requires parent node to delete
Columns such as: Find internal price to delete
NodeList NodeList = document.getElementsByTagName ("Internal price");
Node node = nodelist.item (0);
Node.getpraentnode (). Removechhild (n);
2. Write the memory's document tree back into the XML
Transformerfactory factory=transformerfactory.newinstance ();
Transformer tr=factory. Transformer ();
Tr.transformer (new Domsource (document), New Streamresult ("Src/book.xml"));
Ix. manipulating XML element properties: Printing the first book's Publishing house
1, first to get the first
Node node = document.getelementsbytagname ("book"). Item (0);
2. Print the value of the specified property
Element e = (element) n;
System.out.println (E.getattribute ("Publishing house"));











Analytic method of document tree

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.