Example usage for org.w3c.dom Element getOwnerDocument

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

Introduction

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

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:Main.java

public static void addCdataNode(Element element, String tagName, String value) {
    if (value != null && !(value.equals(""))) {
        Document document = element.getOwnerDocument();
        Element titleElement = document.createElement(tagName);
        titleElement.appendChild(document.createCDATASection(value));
        element.appendChild(titleElement);
    }//w  w  w.  j av a2  s  . c  o  m
}

From source file:Main.java

public static void addTextNode(Element element, String tagName, String value) {
    if (value != null && !(value.equals(""))) {
        Element titleElement = element.getOwnerDocument().createElement(tagName);
        titleElement.setTextContent(value);
        element.appendChild(titleElement);
    }/*from  w w  w .  jav a  2s .c  o  m*/
}

From source file:Main.java

/**
 * Add the element to the parent by the node name
 *
 * @param parent/*ww  w.  j a v a2  s . c o  m*/
 * @param tagName
 */
public static void addElement(Element parent, String tagName) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    parent.appendChild(child);
}

From source file:Main.java

public static Node createNodeFromPath(Element modelElement, String path) {
    Document document = modelElement.getOwnerDocument();
    StringTokenizer st = new StringTokenizer(path, "/");
    while (st.hasMoreTokens()) {
        String t = st.nextToken();

        if (st.hasMoreTokens()) {
            if (t.equals("..")) {
                modelElement = (Element) modelElement.getParentNode();
            } else {
                Element elm = getFirstChildElement(modelElement, t);
                if (elm == null)
                    modelElement = (Element) modelElement.insertBefore(document.createElement(t),
                            getFirstChildElement(modelElement, t));
                else
                    modelElement = elm;/* w w  w  .ja va  2 s.c o  m*/
            }
        } else {
            modelElement = (Element) modelElement.insertBefore(document.createElement(t),
                    getFirstChildElement(modelElement, t));
        }
    }

    return modelElement;
}

From source file:Main.java

public static void appendTextChild(Element parent, String tagname, String text) {
    if (text == null) {
        text = "";
    }/*from w  ww  .j ava  2s . co m*/
    Document doc = parent.getOwnerDocument();
    Element childElement = doc.createElement(tagname);
    Text textNode = doc.createTextNode(text);
    childElement.appendChild(textNode);
    parent.appendChild(childElement);
}

From source file:Main.java

/**
 * Append attribute.//from  w w  w.  ja v a 2s.c  o m
 *
 * @param baseNode
 *            the base node
 * @param attributeName
 *            the attribute name
 * @param attributeValue
 *            the attribute value
 */
public static void appendAttribute(Element baseNode, String attributeName, String attributeValue) {
    Attr typeAttribute = baseNode.getOwnerDocument().createAttribute(attributeName);
    if (attributeValue == null) {
        typeAttribute.setNodeValue("<null>");
    } else {
        typeAttribute.setNodeValue(attributeValue);
    }
    baseNode.setAttributeNode(typeAttribute);
}

From source file:Main.java

/**
 * Convert an XML fragment from one namespace to another.
 *
 * @param from      element to translate
 * @param namespace namespace to be translated to
 * @return the element in the new namespace
 *
 * @since 8.4//from ww  w . j av a  2s  . c o m
 */
public static Element translateXML(Element from, String namespace) {
    Element to = from.getOwnerDocument().createElementNS(namespace, from.getLocalName());
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            newNode = translateXML((Element) node, namespace);
        } else {
            newNode = node.cloneNode(true);
        }
        to.appendChild(newNode);
    }
    NamedNodeMap m = from.getAttributes();
    for (int i = 0; i < m.getLength(); i++) {
        Node attr = m.item(i);
        to.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }
    return to;
}

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  w w w. java 2 s  .  c o 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

/**
 * Processes any element and add them into the element.
 *
 * @param element the element where to add the elements.
 * @param any     the any elements to process.
 * @throws Exception if there is some error during processing.
 *//* w  ww. j  av a 2s. c  om*/
public static void processAny(Element element, List<Element> any) throws Exception {
    for (Element anyElement : any) {
        Node importedElement = element.getOwnerDocument().importNode(anyElement, true);
        element.appendChild(importedElement);
    }
}

From source file:Main.java

private static Attr getOrCreateAttribute(Element parent, String name) {
    Attr a = parent.getAttributeNode(name);
    if (a == null) {
        Document doc = parent.getOwnerDocument();
        a = doc.createAttribute(name);/*w  w w . j  a v  a  2 s .c om*/
        parent.setAttributeNode(a);
    }
    return a;
}