Use JAXP to parse xml documents using Dom

Source: Internet
Author: User

[Java] package cn. xue. xml; import java. io. fileOutputStream; import java. io. IOException; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import javax. xml. transform. transformer; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; 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; // use dom to perform crud public class Test2 on the xml document {// read the xml document: <title> JavaScript webpage development </title> node value @ Test public void read1 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); NodeList list = document. getElementsByTagName ("name"); Node node = list. item (1); String content = node. getTextContent (); System. out. println (content);} // obtain all tags in the xml document @ Test public void read2 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); // get the root Node root = document. getElementsByTagName ("bookshelf "). item (0); 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 the tag attribute value in the xml document: <title name = "xxxx"> Java Employment Training tutorial </title> @ Test public void read3 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); Element bookname = (Element) document. getElementsByTagName ("name "). item (0); String value = bookname. getAttribute ("name"); System. out. println (value);} // Add a node to the xml document: <price> 59.00 RMB </price> @ Test public void add1 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); // create a node Element price = document. createElement ("price"); price. setTextContent ("59.00 RMB"); // The node is mounted to the first book. Element book = (Element) document. getElementsByTagName (" "). item (0); book. appendChild (price); // write the updated memory back to the xml file TransformerFactory tffacloud = TransformerFactory. newInstance (); Transformer tf = tffacloud. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream ("src/book. xml ");} // insert a node to a specified position in the xml document: <price> 59.00 RMB </price> @ Test public void add2 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); // create a node Element price = document. createElement ("price"); price. setTextContent ("59.00 RMB"); // obtain the reference node Element refNode = (Element) document. getElementsByTagName ("price "). item (0); // get the node Element book = (Element) document. getElementsByTagName (" "). item (0); // mount the cursor to the specified position of the book node. insertBefore (price, refNode); // write the updated memory back to the xml file TransformerFactory tffactory = TransformerFactory. newInstance (); Transformer tf = tffacloud. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream ("src/book. xml ");} // Add attributes to the xml document node: <title> Java Employment Training tutorial </title> Add name = "xxxx" attribute @ Test public void addAttr () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); Element bookname = (Element) document. getElementsByTagName ("name "). item (0); bookname. setAttribute ("name", "xxxx"); // write the updated memory back to the xml file TransformerFactory tffactory = TransformerFactory. newInstance (); Transformer tf = tffacloud. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream ("src/book. xml ");} @ Test public void delete1 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); // get the node to be deleted Element e = (Element) document. getElementsByTagName ("price "). item (0); // get the father Element book to delete the node = (Element) document. getElementsByTagName (" "). item (0); // Delete the child book again. removeChild (e); // write the updated memory back to the xml file TransformerFactory tffacloud = TransformerFactory. newInstance (); Transformer tf = tffacloud. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream ("src/book. xml ");} // Delete the entire xml document @ Test public void delete2 () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); // get the node to be deleted Element e = (Element) document. getElementsByTagName ("price "). item (0); e. getParentNode (). getParentNode (). getParentNode (). removeChild (e. getParentNode (). getParentNode (); // write the updated memory back to the xml file TransformerFactory tffacloud = TransformerFactory. newInstance (); Transformer tf = tffacloud. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream ("src/book. xml ");} @ Test public void update () throws Exception {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory. newDocumentBuilder (); Document document = builder. parse ("src/book. xml "); Element e = (Element) document. getElementsByTagName ("price "). item (0); e. setTextContent ("109.00 RMB"); // write the updated memory back to the xml file TransformerFactory tffacloud = TransformerFactory. newInstance (); Transformer tf = tffacloud. newTransformer (); tf. transform (new DOMSource (document), new StreamResult (new FileOutputStream ("src/book. 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.