I joined the blog Park on the first day. I am very happy to learn from each other. Recently, I learned how to parse xml using dom in my video. You can share it with me. Thank you for your support. domxml

Source: Internet
Author: User

I joined the blog Park on the first day. I am very happy to learn from each other. Recently, I learned how to parse xml using dom in my video. You can share it with me. Thank you for your support. domxml
Dom Parsing

Public static Document getDocument () throws Exception {
// 1. Create a factory
DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
// 2. Get the dom parser
DocumentBuilder builder = factory. newDocumentBuilder ();
// 3. parse the xml document and obtain the document
Return builder. parse (filename); // filename indicates the path of the parsed xml document.

}

Xml document

<? Xml version = "1.0" encoding = "UTF-8" standalone = "no"?> <Bookshelves>
<Book>
<Title name = "xxx"> Java employment training course </title>
<Author> Zhang Xiaoxiang </author>
<Price> 59.00 RMB </price>
</Book>
<Book>
<Title> JavaScript web development </title>
<Author> Zhang Xiaoxiang </author>
<Price> 28.00 RMB </price>
</Book>
</Bookshelves>

Read operation 1

NodeList list = document. getElementsByTagName ("name ");
Node node = list. item (1 );
String content = node. getTextContent ();
System. out. println (content );

Read operation 2 traverses all nodes

Node root = document. getElementsByTagName ("bookshelf"). item (0); // get the root Node
List (root );

Private void list (Node node ){
If (node instanceof Element ){
System. out. println (node. getNodeName ());
}
NodeList list = node. getChildNodes ();
For (int I = 0; I <list. getLength (); I ++ ){

Node child = list. item (I );
List (child );
}

}

Get target node attributes

Element bookname = (Element) document. getElementsByTagName ("name"). item (0 );
String value = bookname. getAttribute ("name ");
System. out. println (value );

Add Node

// Create a node
Element price = document. createElement ("price ");
Price. setTextContent ("$59.00 ");

// Create a node and mount the first book
Element book = (Element) document. getElementsByTagName (""). item (0 );
Book. appendChild (price );

// Write the updated data back to the document

TransformerFactory tffacloud = TransformerFactory. newInstance ();
Transformer tf = tffacloud. newTransformer ();
Tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream (filename )));

Insert at specified position

// Create a node
Element price = document. createElement ("price ");
Price. setTextContent ("$59.00 ");
// Obtain the reference node
Element refNode = (Element) document. getElementsByTagName ("price"). item (0 );
// Obtain the mounted Node
Element book = (Element) document. getElementsByTagName (""). item (0 );
// Insert data to the specified position of the book Node
Book. insertBefore (price, refNode );

// Write the updated data back to the document

TransformerFactory tffacloud = TransformerFactory. newInstance ();
Transformer tf = tffacloud. newTransformer ();
Tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream (filename )));

Add node attributes

Element bookname = (Element) document. getElementsByTagName ("name"). item (0 );
Bookname. setAttribute ("name", "xxxxxx ");

Delete a node

// Get the node to be deleted
Element element = (Element) document. getElementsByTagName ("price"). item (0 );

Element. getParentNode (). removeChild (element );

// Write the updated data back to the document

TransformerFactory tffacloud = TransformerFactory. newInstance ();
Transformer tf = tffacloud. newTransformer ();
Tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream (filename )));

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.