Example usage for org.w3c.dom Node replaceChild

List of usage examples for org.w3c.dom Node replaceChild

Introduction

In this page you can find the example usage for org.w3c.dom Node replaceChild.

Prototype

public Node replaceChild(Node newChild, Node oldChild) throws DOMException;

Source Link

Document

Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main example = new Main();
    example.data.put("France", "Paris");
    example.data.put("Japan", "Tokyo");

    JAXBContext context = JAXBContext.newInstance(Main.class);
    Marshaller marshaller = context.createMarshaller();
    DOMResult result = new DOMResult();
    marshaller.marshal(example, result);

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    Document document = (Document) result.getNode();
    XPathExpression expression = xpath.compile("//map/entry");
    NodeList nodes = (NodeList) expression.evaluate(document, XPathConstants.NODESET);

    expression = xpath.compile("//map");
    Node oldMap = (Node) expression.evaluate(document, XPathConstants.NODE);
    Element newMap = document.createElement("map");

    for (int index = 0; index < nodes.getLength(); index++) {
        Element element = (Element) nodes.item(index);
        newMap.setAttribute(element.getAttribute("key"), element.getAttribute("value"));
    }/*from  w ww  . j av a2  s  . c o  m*/

    expression = xpath.compile("//map/..");
    Node parent = (Node) expression.evaluate(document, XPathConstants.NODE);
    parent.replaceChild(newMap, oldMap);

    TransformerFactory.newInstance().newTransformer().transform(new DOMSource(document),
            new StreamResult(System.out));
}

From source file:Main.java

public static void replaceNode(Node oldNode, Node newNode, Node source) {
    source.replaceChild(newNode, oldNode);
}

From source file:Main.java

/**
 * Replace the old node with the new node.
 * //from  w ww .j a  va  2 s  .co m
 * @param parent
 *          the node parent
 * @param oldNode
 *          the old node
 * @param newNode
 *          the new node
 */
public static void replaceNode(Node parent, Node oldNode, Node newNode) {
    parent.replaceChild(newNode, oldNode);
}

From source file:Main.java

/**
 * Will create a documentFragment of the replacingDocument, will import the
 * replacingDocument as a node of the replacedDocument, and then will
 * replace the replaceNode with the documentFragment of replacingDocument.
 * //from w ww .  j ava  2 s  . c o m
 * @param replacedDocument
 *            The document which will have a node replace
 * @param replacingDocument
 *            The document that will replace a node
 * @param replacedNode
 *            The node in replacedDocument that will be replaced
 * @return The new version of replacedDocument will replacedNode replaced
 */
public static Node replaceNode(Document replacedDocument, Document replacingDocument, Node replacedNode) {

    // Create a documentFragment of the replacingDocument
    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    // Import docFrag under the ownership of replacedDocument
    Node replacingNode = ((replacedDocument).importNode(docFrag, true));

    // In order to replace the node need to retrieve replacedNode's parent
    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
}

From source file:Main.java

/**
 * Will create a documentFragment of the replacingDocument, will import the
 * replacingDocument as a node of the replacedDocument, and then will
 * replace the replaceNode with the documentFragment of replacingDocument.
 * /*from w  w w.java  2s  . c o m*/
 * @param replacedDocument
 *            The document which will have a node replace
 * @param replacingDocument
 *            The document that will replace a node
 * @param replacedNode
 *            The node in replacedDocument that will be replaced
 * @return The new version of replacedDocument will replacedNode replaced
 */
public static Node replaceNode(final Document replacedDocument, final Document replacingDocument,
        final Node replacedNode) {

    // Create a documentFragment of the replacingDocument
    DocumentFragment docFrag = replacingDocument.createDocumentFragment();
    Element rootElement = replacingDocument.getDocumentElement();
    docFrag.appendChild(rootElement);

    // Import docFrag under the ownership of replacedDocument
    Node replacingNode = ((replacedDocument).importNode(docFrag, true));

    // In order to replace the node need to retrieve replacedNode's parent
    Node replaceNodeParent = replacedNode.getParentNode();
    replaceNodeParent.replaceChild(replacingNode, replacedNode);
    return replacedDocument;
}

From source file:Main.java

/**
 * Changes the tag name of an element.//  w w w  .ja v  a 2s. c o m
 * 
 * @param elem Element which name should be changed
 * @param newName new tag name of the element
 * @return true if the name was changed successfully or false otherwise
 */
static public boolean changeTagName(Element elem, String newName) {
    if (elem == null)
        return false; // not Found!
    Document doc = elem.getOwnerDocument();
    Element newElem = doc.createElement(newName);

    // Copy the attributes to the new element
    NamedNodeMap attrs = elem.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr2 = (Attr) doc.importNode(attrs.item(i), true);
        newElem.getAttributes().setNamedItem(attr2);
    }

    // Copy all Child Elements
    for (Node node = elem.getFirstChild(); node != null; node = node.getNextSibling())
        newElem.appendChild(node.cloneNode(true));
    // insert
    Node parent = elem.getParentNode();
    parent.replaceChild(newElem, elem);
    return true;
}

From source file:Main.java

public static Node replaceNode(Document ImpD, Node oldE, Node newE) {
    Node Parent = oldE.getParentNode();
    Node ImpNewNode = ImpD.importNode(newE, true);
    Parent.replaceChild(ImpNewNode, oldE);

    return Parent;
}

From source file:Main.java

public static Node replaceNode(final Document impD, final Node oldE, final Node newE) {
    Node parent = oldE.getParentNode();
    Node impNewNode = impD.importNode(newE, true);
    parent.replaceChild(impNewNode, oldE);

    return parent;
}

From source file:Main.java

public static void replaceNode(Document doc, Node parent, Node oldNode, Node newNode) {
    Node comment = doc.createComment(" Updated by FM GUI @ " + (new Date()) + " ");
    parent.insertBefore(comment, oldNode);
    parent.replaceChild(newNode, oldNode);
    formatNode(doc, parent, newNode);/*from w w w  .j a  va2  s. c om*/
}

From source file:Main.java

/**
 * This method is used for updating the value of a tag in a
 * <code>Document</code> object.
 * //w w  w .ja  v  a  2  s.c  o  m
 * @param doc
 *            Document object
 * @param tagName
 *            name of the tag
 * @param tagValue
 *            the updated value of the tag
 */
public static void replaceTagValue(Document doc, String tagName, String tagValue) {
    NodeList nodeList = doc.getElementsByTagName(tagName);
    int j = nodeList.getLength();
    Node node;
    for (int i = 0; i < j; i++) {
        Node newNode = doc.createTextNode(tagValue);
        node = nodeList.item(i);
        if (node.getFirstChild() != null) {
            node.replaceChild(newNode, node.getFirstChild());
        } else {
            node.appendChild(newNode);
        }
    }
}