DOM add/update/delete XML (CURD)

Source: Internet
Author: User

Get Document

/**
* Getting documents
* 1. Obtain the instance factory
* 2. Get the parser
* 3. Obtain the document
*/

 

Add node

/**
* 1. Obtain the root node.
* 2. Create a node (set attributes)
* 3. Create a subnode ()
* 4. Add to the root node
*/

Update node

/**
* Update XML
* 1. Obtain all nodes
* 2. Obtain each book
* 3. Obtain the ID
* 4. Update
** 1. Update the id
** 2. Update a subnode
*/

Delete a node

/**
* Delete XML
* 1. Obtain all nodes
* 2. Obtain each book
* 3. Obtain the ID
* 4. Delete
*/

Save XML

/**
* Save xml
* 1. Obtain the persistent object instance factory
* 2. Obtain a persistent object
* 3. Save the memory data to the disk.
** Source ---- encapsulate the document object to the Source
** Result ---- encapsulate the file path to Result
* 4. Save
*/

 

1 public static void main (String [] args) throws Exception {2 // get document 3 Document document = getDocument (); 4 // update xml 5 // UpdateXml (document ); 6 7 // add XML 8 9 // AddXML (document); 10 11 12 // delete xml 13 // DeleXML (document ); 14 15 16 17/** 18 * Save 19 */20 Save (document ); 21 22} 23 24/** 25*1. Obtain the root node 26*2. Create a node (set attributes) 27*3. Create a subnode () 28*4. Add to root node 29 */30 private static void AddXML (Document document) {31 Element rootElement = document. getDocumentElement (); 32 33 Element newElement = document. createElement ("book"); 34 35 newElement. setAttribute ("id", "b003"); 36 37 Element PriceElement = document. createElement ("title"); 38 PriceElement. setTextContent ("30"); 39 PriceElement. setAttribute ("unit", "¥"); 40 newElement. appendChild (PriceElement); 41 42 rootElement. appendChild (newElement ); 43} 44 45/** 46 * delete XML 47*1. Get all nodes 48*2. Get 49*3. Get ID 50*4. Delete 51 */52. 53 private static void DeleXML (Document document) {54 NodeList nodeList = document. getElementsByTagName ("book"); 55 for (int I = 0; I <nodeList. getLength (); I ++) {56 Node chilNode = nodeList. item (I); 57 String id = (Element) chilNode ). getAttribute ("id"); 58 if ("b001 ". equals (id) 59 {60 Node parentNode = chilNode. getParentNode (); 61 parentNode. removeChild (chilNode ); 62} 63} 64} 65 66/** 67 * update XML 68*1. Get all nodes 69*2. Get 70*3. Get ID 71*4. Update 72 ** 1. Update id 73 ** 2. Update subnode 74 */75 private static void UpdateXml (Document document) {76 NodeList nodeList = document. getElementsByTagName ("book"); 77 for (int I = 0; I <nodeList. getLength (); I ++) {78 Node node = nodeList. item (I); 79 Element element = (Element) node; 80 String id = element. getAttribute ("id"); 81 if ("b001 ". equals (id) 82 {83 // element. setAttribute ("id", "booooo"); // update the id value 84 NodeList childNode = element. getElementsByTagName ("title"); 85 Element childElement = (Element) childNode. item (0); // childNode. getLength () = 1 86 childElement. setTextContent ("Java core technology "); 87} 88} 89} 90 91/** 92 * save xml 93*1. Get the persistence object instance factory 94*2. Get the persistence object 95*3. Save the memory data disk 96 ** Source ---- encapsulate document object to Source 97 ** Result ---- encapsulate file path to Result 98*4, Save 99 */100 private static void Save (Document document) 101 throws Exception {102 TransformerFactory factory = TransformerFactory. newInstance (); 103 Transformer transformer = factory. newTransformer (); 104 Source xmlSource = new DOMSource (document); 105 javax. xml. transform. result outputTarget = new StreamResult ("books. jaxp. xml "); 106 107 transformer. transform (xmlSource, outputTarget); 108 109 System. out. println ("done "); 110} 111 112/** 113 * get Document 114*1. Get instance factory 115*2. Get parser 116*3. Get document117 */118 119 private static Document getDocument () throws ParserConfigurationException, 120 SAXException, IOException {121 DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); 122 DocumentBuilder builder = factory. newDocumentBuilder (); 123 Document document = builder. parse ("books. xml "); 124 return document; 125}

 

DOM add/update/delete XML (CURD)

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.