How to update nodes in xml
This article mainly shows you "how to update nodes in xml", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to update nodes in xml" this article.
Update the instance code of the node to xml
/ * System.out.println ("2323"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder docbuilder = factory.newDocumentBuilder (); Document parse = docbuilder.parse (new File ("src/ProdQuery.xml")); / / Element createElement2 = parse.createElement (""); Element createElement = parse.createElement ("products"); Node item2 = parse.getChildNodes (). Item (parse.getChildNodes (). GetLength ()-1); for (String item: map.keySet ()) {/ / loop added to productsSystem.out.println ("tianjia1") Element clidren = parse.createElement ("product"); Attr createAttribute = parse.createAttribute ("name"); createAttribute.setNodeValue (item); Attr createAttribute2 = parse.createAttribute ("value"); createAttribute2.setNodeValue (map.get (item)); clidren.setAttributeNode (createAttribute); clidren.setAttributeNode (createAttribute2); createElement.appendChild (clidren);} / / add products to the root directory item2.appendChild (createElement); / / create an TransformerFactory object TransformerFactory tFactory = TransformerFactory.newInstance (); / / get an Operand Transformer transformer = tFactory.newTransformer () / / Settings, this is the code transformer.setOutputProperty (OutputKeys.ENCODING, "UTF-8"); / / DOMSource source = new DOMSource (parse); System.out.println ("change file"); StreamResult result = new StreamResult (new File ("src/ProdQuery.xml")); / / wrap xml files to transformer.setOutputProperty (OutputKeys.INDENT, "yes"); transformer.setOutputProperty ("{http://xml.apache.org/xslt}indent-amount"," 4 "); transformer.transform (source, result) * / above are all the contents of the article "how to update nodes in xml". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!