Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; public class Main { /** * Update a property of a given configuration file and return a string representation of the * xml document * @param doc * @param property * @param newValue * @throws IllegalAccessException * @throws InstantiationException * @throws ClassNotFoundException */ public static String updateXmlDoc(Document doc, List<String> properties) throws ClassNotFoundException, InstantiationException, IllegalAccessException { for (String property : properties) { String key = property.split(":")[0]; String value = property.split(":")[1]; NodeList propertiesNode = doc.getElementsByTagName("esf:property"); for (int i = 0; i < propertiesNode.getLength(); i++) { Node node = propertiesNode.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element el = (Element) node; if (key.equals(el.getAttribute("name"))) { el.getElementsByTagName("esf:value").item(0).setTextContent(value); } } } } return parseXmlDocToString(doc); } private static String parseXmlDocToString(Document xmlDoc) throws ClassNotFoundException, InstantiationException, IllegalAccessException { // Add formatting to the xml document in case we want to save to a file System.setProperty(DOMImplementationRegistry.PROPERTY, "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl"); final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance(); final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS"); final LSSerializer writer = impl.createLSSerializer(); writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified. writer.getDomConfig().setParameter("xml-declaration", true); // Set this to true if the declaration is needed to be outputted return writer.writeToString(xmlDoc); } }