Java tutorial
//package com.java2s; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; public class Main { public static String replaceXPath(Document domDocument, String search, String replace) { try { NodeList nodes = getXPathNodes(search, domDocument); for (int i = 0; i < nodes.getLength(); i++) { //logger.debug("Replacing"); Node node = nodes.item(i); node.setTextContent(replace); } return getDomDocumentAsXml(domDocument); } catch (Exception e) { e.printStackTrace(); return null; } } public static NodeList getXPathNodes(String search, String file) { return getXPathNodes(search, getDomDocument(file)); } public static NodeList getXPathNodes(String search, Document domDocument) { return getXPathNodes(search, domDocument, getXPathInstance()); } public static NodeList getXPathNodes(String search, Document domDocument, XPath xpathInstance) { try { XPathExpression expr = xpathInstance.compile(search); return (NodeList) expr.evaluate(domDocument, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); return null; } } public static String getDomDocumentAsXml(org.w3c.dom.Document domDocument) { try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult xmlOutput = new StreamResult(new StringWriter()); transformer.transform(new DOMSource(domDocument), xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); return null; } } public static Document getDomDocument(String source) { return getDomDocument(source, true); } public static Document getDomDocument(String source, boolean sourceIsFile) { try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); // NEVER FORGET THIS DocumentBuilder builder = domFactory.newDocumentBuilder(); if (sourceIsFile == false) { return builder.parse(new InputSource(new StringReader(source))); } else { return builder.parse(source); } } catch (IOException e) { e.printStackTrace(); return null; } catch (ParserConfigurationException e) { e.printStackTrace(); return null; } catch (SAXException e) { e.printStackTrace(); return null; } } public static XPath getXPathInstance() { XPathFactory factory = XPathFactory.newInstance(); return factory.newXPath(); } }