Example usage for org.w3c.dom Element cloneNode

List of usage examples for org.w3c.dom Element cloneNode

Introduction

In this page you can find the example usage for org.w3c.dom Element cloneNode.

Prototype

public Node cloneNode(boolean deep);

Source Link

Document

Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);// w w w.  ja  va2s .c o m

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    NodeList list = doc.getElementsByTagName("entry");
    Element element = (Element) list.item(0);

    Element dup = (Element) element.cloneNode(true);

    element.getParentNode().insertBefore(dup, element.getNextSibling());
}

From source file:Main.java

public static void edit(Document doc) {
    NodeList list = doc.getElementsByTagName("name");
    Element element = (Element) list.item(0);

    Element dup = (Element) element.cloneNode(true);

    element.getParentNode().insertBefore(dup, element.getNextSibling());
}

From source file:Main.java

public static void duplicatePerson(Document doc) {
    Element root = doc.getDocumentElement();
    Element origPerson = (Element) root.getFirstChild();
    Element newPerson = (Element) origPerson.cloneNode(true);
    root.appendChild(newPerson);/*  ww  w. j av a2 s .  c om*/
}

From source file:Main.java

/**
 * Creates a document that has a deep copy of the element passed in as its document element
 * @param element// ww  w.  j a  v a  2s. c  o m
 * @return the new document
 */
public static Document createDocumentFrom(Element element) {
    Document document = createEmptyDocument();
    Node clonedNode = element.cloneNode(true);
    clonedNode = document.adoptNode(clonedNode);
    document.appendChild(clonedNode);
    return document;
}

From source file:Main.java

public static String getNodeTextByXPath(Element eoEl, String xpath) {
    try {/*from  w  ww.j a va2s.  co  m*/
        Document doc = newDocument();
        Element importedEl = (Element) doc.importNode(eoEl.cloneNode(true), true);
        doc.appendChild(importedEl);

        XPathFactory factory = XPathFactory.newInstance();
        XPath xpathEn = factory.newXPath();
        return xpathEn.evaluate(xpath, doc);
    } catch (Exception ex) {
        ex.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static Element cloneElementAs(Element srcEl, Document dstDoc, String elName) {
    if (srcEl.getNodeName().equals(elName)) {
        if (srcEl.getOwnerDocument() == dstDoc) {
            return (Element) srcEl.cloneNode(true);
        } else {/*from  www.  j a  va 2  s.co  m*/
            return (Element) dstDoc.importNode(srcEl, true);
        }
    } else {
        final Element dstEl = dstDoc.createElement(elName);
        final NodeList srcChildren = srcEl.getChildNodes();
        final int n = srcChildren.getLength();
        for (int i = 0; i < n; ++i) {
            final Node srcChild = srcChildren.item(i);
            final Node dstChild = dstDoc.importNode(srcChild, true);
            dstEl.appendChild(dstChild);
        }
        return dstEl;
    }
}

From source file:Main.java

private static Element fixupAttrs(Element root) { // #140905
    // #6529766/#6531160: some versions of JAXP reject attributes set using setAttribute
    // (rather than setAttributeNS) even though the schema calls for no-NS attrs!
    // JDK 5 is fine; JDK 6 broken; JDK 6u2+ fixed
    // #146081: xml:base attributes mess up validation too.
    Element copy = (Element) root.cloneNode(true);
    fixupAttrsSingle(copy);//www  .j  av a  2 s. co  m
    NodeList nl = copy.getElementsByTagName("*"); // NOI18N
    for (int i = 0; i < nl.getLength(); i++) {
        fixupAttrsSingle((Element) nl.item(i));
    }
    return copy;
}

From source file:com.wavemaker.tools.ws.XJCCompiler.java

private static Element removeImportElement(Element element) {
    NodeList nodeList = element.getElementsByTagNameNS(Constants.XSD_NS, "import");
    if (nodeList.getLength() == 0) {
        return element; // simply returns the original one
    }/*from  w w w  .java2  s .c  o  m*/

    // do a clone since we are going to remove the import stuffs from the
    // element
    Element elementClone = (Element) element.cloneNode(true);
    nodeList = elementClone.getElementsByTagNameNS(Constants.XSD_NS, "import");
    List<Node> ns = new ArrayList<Node>();
    for (int tmp = 0; tmp < nodeList.getLength(); tmp++) {
        Node importNode = nodeList.item(tmp);
        ns.add(importNode);
    }
    for (Node item : ns) {
        Node schemaNode = item.getParentNode();
        schemaNode.removeChild(item);
    }
    return elementClone;
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Generates an XML document from list of elements
 *
 * @param elements//from w w w  .j  a va  2s  . c o  m
 * @return
 */
public static Document getDocumentFromList(List<Element> elements) throws MetsExportException {
    Document document = null;
    try {
        DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
        builder.setValidating(true);
        builder.setNamespaceAware(true);
        document = builder.newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e1) {
        throw new MetsExportException("Error while getting document from list", false, e1);
    }

    for (Element element : elements) {
        Node newNode = element.cloneNode(true);
        document.adoptNode(newNode);
        document.appendChild(newNode);
    }
    return document;
}

From source file:cz.cas.lib.proarc.common.export.mets.MetsUtils.java

/**
 *
 * Returns a node from the xml document defined by the Xpath
 *
 * @param elements//from ww  w. j  av a  2s.  co m
 * @param xPath
 * @return
 */
public static Node xPathEvaluateNode(List<Element> elements, String xPath) throws MetsExportException {
    Document document = null;
    try {
        document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e1) {
        throw new MetsExportException("Error while evaluating xPath " + xPath, false, e1);
    }

    for (Element element : elements) {
        Node newNode = element.cloneNode(true);
        document.adoptNode(newNode);
        document.appendChild(newNode);
    }
    XPath xpathObject = XPathFactory.newInstance().newXPath();

    try {
        return (Node) xpathObject.compile(xPath).evaluate(document, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        throw new MetsExportException("Error while evaluating xPath " + xPath, false, e);
    }
}