Java Utililty Methods XML Node Replace

List of utility methods to do XML Node Replace

Description

The list of methods to do XML Node Replace are organized into topic(s).

Method

StringreplaceComma(Node node)
replace Comma
if (node.getFirstChild().getTextContent() == null) {
    return "";
String[] ss = node.getFirstChild().getTextContent().split(",");
if (ss.length == 1) {
    return ss[0] + " " + ss[0];
return ss[0] + " " + ss[1];
...
voidreplaceNode(Node masterNode, Node oldNode, Node newNode)
Replaces all occurrences of oldNode in masterNode with newNode.
for (int i = 0; i < masterNode.getChildNodes().getLength(); i++) {
    Node n = masterNode.getChildNodes().item(i);
    replaceNode(n, oldNode, newNode);
    if (n.getNodeType() == Node.TEXT_NODE && oldNode.getNodeType() == Node.TEXT_NODE
            && n.getNodeValue().equals(oldNode.getNodeValue())) {
        masterNode.insertBefore(newNode.cloneNode(true), n);
        masterNode.removeChild(n);
    } else if (n.getNodeType() != Node.TEXT_NODE && n.getNodeName().equals(oldNode.getNodeName())
...
voidreplaceNode(Node oldNode, Node newNode, Node source)
replace Node
source.replaceChild(newNode, oldNode);
voidreplaceText(Node node, String text)
replace Text
for (;;) {
    Node n = node.getFirstChild();
    if (n == null)
        break;
    node.removeChild(n);
Text t = node.getOwnerDocument().createTextNode(text);
node.appendChild(t);
...
voidreplaceVariable(final Node node, final String sVar, final String sValue)
replace Variable
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);
...
voidreplaceVariable(final Node node, final String var, final String valueString)
replace Variable
switch (node.getNodeType()) {
case 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("$(" + var + ")")) {
            String att = attr.getValue();
...
voidreplaceWith(Node currentNode, Node replacerNode)
replace With
currentNode.getParentNode().replaceChild(replacerNode, currentNode);
voidreplaceWith(Node oldNode, Node newNode)
Replaces the old node with the new node
Node parentNode = oldNode.getParentNode();
if (parentNode != null) {
    parentNode.replaceChild(newNode, oldNode);