Here you can find the source of replaceVariable(final Node node, final String sVar, final String sValue)
Parameter | Description |
---|---|
node | the node to do variable replacement in |
sVar | the variable name to replace |
sValue | the value to replace the variable name with |
public static void replaceVariable(final Node node, final String sVar, final String sValue)
//package com.java2s; //License from project: Open Source License import org.w3c.dom.*; public class Main { /**//from w w w . j av a2s . co m * @param node the node to do variable replacement in * @param sVar the variable name to replace * @param sValue the value to replace the variable name with */ public static void replaceVariable(final Node node, final String sVar, final String sValue) { if (node.getNodeType() == Node.ELEMENT_NODE) { final Element element = (Element) node; final NamedNodeMap atts = element.getAttributes(); for (int i = 0; i < atts.getLength(); i++) { final Attr attr = (Attr) atts.item(i); if (attr.getValue().contains("$(" + sVar + ")")) { String sAtt = attr.getValue(); sAtt = sAtt.replaceAll("\\$\\(" + sVar + "\\)", sValue); attr.setNodeValue(sAtt); } } } // process children final NodeList children = node.getChildNodes(); for (int iChild = 0; iChild < children.getLength(); iChild++) { final Node child = children.item(iChild); replaceVariable(child, sVar, sValue); } } }