Disclaimer: This article is the learning process of watching Fang Lixun's video of Chuanzhi Podcast. It is very detailed and makes a summary.
One: Before parsing the xml document object, you need to obtain the document object. This part is the template code
//1: Create a factory that enables an application to get a parser that generates a tree of DOM objects from an XML document DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //2: get the dom parser DocumentBuilder builder = factory.newDocumentBuilder(); //3: Parse the xml document and get the document representing the document Document doc = builder.parse("src/Book.xml");
After many of them operate on the document, the part that needs to update the document is also the template code.
//Write the updated memory to the document TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("src/Book.xml")));
Of course, these codes should be placed in a tool class, but I am too lazy to do it, and the following code looks bloated.
Two: After obtaining the document object, it is the CRUD process of the document.
Let me first say a very important thing: the class content in the XML document has corresponding objects : the label ELEMENT object, the text becomes a Text object, and the attribute Attri object. But no matter what the object is, it is a subclass of Node, so any node obtained can be regarded as Node in development. Proper conversion is important.
Documentation added:
a: add node
//add node @Test public void add() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); //create node Element price = doc.createElement("售价"); price.setTextContent("59.00"); //Hang the created node to the first book Element book = (Element) doc.getElementsByTagName("书").item(0); book.appendChild(price); //Write the updated memory back to the document TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("src/Book.xml"))); }
Note here that after adding a node, the updated memory must be written back to the document
b: add the node to the specified position
//Add a node to the specified location in the document @Test public void add2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); //create node Element price = doc.createElement("售价"); price.setTextContent("59.00"); // get the reference node Element refnode = (Element) doc.getElementsByTagName("售价").item(0); //Get the node to hang Element book = (Element) doc.getElementsByTagName("书").item(0); //insert book.insertBefore(price, refnode); //Write the updated memory to the document TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("src/Book.xml"))); }
c: add attribute
[java] view plaincopy //Add properties to the document @Test public void addAttribute() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); Element element = (Element) doc.getElementsByTagName("书名").item(0); element.setAttribute("name", "xxxx"); //Write the updated memory to the document TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("src/Book.xml"))); }
Read documentation:
a: get the content of the specified tag
@Test public void Read1() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); NodeList list = doc.getElementsByTagName("书名"); Node node = list.item(1);//Start from 0 String content = node.getTextContent(); System.out.println(content); }
b: Traverse all tags, use recursion here.
@Test public void Read2() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); // get the root node Node root = doc.getElementsByTagName("书架").item(0); list(root);//Recursive print } 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); } }
c: Get the attributes of the specified label, which uses the conversion of Node--->Eelement
@Test public void Read3() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); Element node = (Element) doc.getElementsByTagName("书名").item(0); String value = node.getAttribute("name"); System.out.println(value); }
3: delete:
@Test public void delete() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); // get the node to delete Element e = (Element) doc.getElementsByTagName("售价").item(0); //Get the parent node of the node to be deleted Element book = (Element) e.getParentNode(); //delete book.removeChild(e);//或者e.getParentNode().removeChild(e); //Write the updated memory to the document TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("src/Book.xml"))); }
4: Change
@Test public void update() throws Exception{ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("src/Book.xml"); // get the node to update Element e = (Element) doc.getElementsByTagName("售价").item(0); //Write the updated memory to the document TransformerFactory tffactory = TransformerFactory.newInstance(); Transformer tf = tffactory.newTransformer(); tf.transform(new DOMSource(doc),new StreamResult(new FileOutputStream("src/Book.xml"))); }
Three: Summary
Through the above code demonstration, you can feel that there are more template codes. It is to get the document fixedly and write it back to the document.
Then the important highlights repeat:
1: The class content in the XML document has corresponding objects: the label ELEMENT object, the text becomes a Text object, and the attribute Attri object. But no matter what the object is, it is a subclass of Node, so any node obtained can be regarded as Node in development.
2: Recursively traverse all tags.
Example
import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.junit.BeforeClass; import org.junit.Test; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * @author forlab * @version 2012-12-19 */ public class Dom { @BeforeClass public static void setUpBeforeClass() throws Exception { } @Test public void readXml() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory db = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder = db.newDocumentBuilder(); Document dom = dbuilder.parse("src/dom.xml"); Node node = dom.getElementsByTagName("书架").item(0); list(node); } private void list(Node node) { if(node instanceof Element){ System.out.println(node.getNodeName()); } NodeList nodes = node.getChildNodes(); for(int i=0;i<nodes.getLength();i++){ Node child = nodes.item(i); list(child); } } @Test public void readXmlByAttrubuteName() throws ParserConfigurationException, SAXException, IOException{ DocumentBuilderFactory db = DocumentBuilderFactory.newInstance(); DocumentBuilder dbuilder = db.newDocumentBuilder(); Document dom = dbuilder.parse("src/dom.xml"); Element element = (Element) dom.getElementsByTagName("作者").item(0); System.out.println(element.getAttribute("name")); } }