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 )));