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

/**
 * add element to parent withe the specified value by the node name
 *
 * @param parent/*w w w  . ja  v  a 2  s .  co  m*/
 * @param tagName
 * @param text
 */
public static void addElement(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    setElementValue(child, text);
    parent.appendChild(child);
}

From source file:Main.java

public static void appendString(Element xmlElement, String tagName, String value) {
    if (value == null)
        return;/*from  www. j ava2s  .  c  o m*/
    Document parentDocument = xmlElement.getOwnerDocument();
    Element newElement = parentDocument.createElement(tagName);
    newElement.setTextContent(value);
}

From source file:Main.java

/**
 * Copy elements from one document to another attaching at the specified
 * element and translating the namespace.
 *
 * @param from         copy the children of this element (exclusive)
 * @param to           where to attach the copied elements
 * @param newNamespace destination namespace
 *
 * @since 8.4/*from w w  w . j  a  v  a  2  s .  c o  m*/
 */
public static void copyDocument(Element from, Element to, String newNamespace) {
    Document doc = to.getOwnerDocument();
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        Node node = nl.item(i);
        Node newNode = null;
        if (Node.ELEMENT_NODE == node.getNodeType()) {
            Element oldElement = (Element) node;
            newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
            NamedNodeMap m = oldElement.getAttributes();
            Element newElement = (Element) newNode;
            for (int index = 0; index < m.getLength(); index++) {
                Node attr = m.item(index);
                newElement.setAttribute(attr.getNodeName(), attr.getNodeValue());
            }
            copyDocument(oldElement, newElement, newNamespace);
        } else {
            newNode = node.cloneNode(true);
            newNode = to.getOwnerDocument().importNode(newNode, true);
        }
        if (newNode != null) {
            to.appendChild(newNode);
        }
    }
}

From source file:Main.java

/**
 *
 * @param rootElement// w w w .  j a  va 2 s  .c  om
 * @param setXmlDecl
 * @return
 */
public static String serializeToString(Element rootElement, boolean setXmlDecl) {
    DOMImplementationLS lsImpl = (DOMImplementationLS) rootElement.getOwnerDocument().getImplementation()
            .getFeature("LS", "3.0");
    LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get
    // String without
    // xml-declaration
    return serializer.writeToString(rootElement);
}

From source file:Main.java

/**
 * Adds data under the given node, ie://  www  .j a  v a 2  s . c  o  m
 * <p/>
 * {@code
 * <Parent></Parent>>
 * }
 * <p/>
 * becomes...
 * <p/>
 * {@code
 * <Parent>
 *     <nodeString>
 *         nodeData
 *     </nodeString>
 * </Parent>
 * }
 *
 * @param parentNode the node to add the data to.
 * @param nodeString the name of the data node to put under the parent node.
 * @param nodeData the data to put in the data node.
 */
public static void addDataUnderNode(Element parentNode, String nodeString, String nodeData) {
    // Create an element for this data type
    Element dataElement = parentNode.getOwnerDocument().createElement(nodeString);

    // Fill element with nodeData
    dataElement.appendChild(parentNode.getOwnerDocument().createTextNode(nodeData));

    parentNode.appendChild(dataElement);
}

From source file:Main.java

/**
 * Appends a child element to the provided parent element.
 * This child element is created with the name and string value provided.
 *
 * @param parentElement the parent element
 * @param name the name for the child element to be created
 * @param value the string value to be assigned to the created child element
 * @return the newly created child element 
 *//*from   w  ww .ja  v  a  2  s .c  o m*/
public static Element appendChildElement(final Element parentElement, final String name, final String value) {
    Document parentDocument = parentElement.getOwnerDocument();
    Element createElement = createElement(parentDocument, name);
    if (value != null) {
        createElement.appendChild(parentDocument.createTextNode(value));
    }
    parentElement.appendChild(createElement);
    return createElement;
}

From source file:Main.java

public static Element createElement(Element parent, String path) {
    int i = path.indexOf('.');
    if (i < 0) {
        Element element = parent.getOwnerDocument().createElement(path);
        parent.appendChild(element);/*  w w  w .j av a 2  s  . co  m*/
        return element;
    }
    String p = path.substring(0, i), c = path.substring(i + 1);
    Element pe = getUniqueChild(parent, p);
    if (pe == null) {
        pe = parent.getOwnerDocument().createElement(p);
        parent.appendChild(pe);
    }
    return createElement(pe, c);
}

From source file:Main.java

public static Element appendCDATAElement(Element parent, String tagName, String value) {
    Element child = appendElement(parent, tagName);
    if (value == null) {
        value = "";
    }/*from  w  w  w .  ja  v  a  2 s.  c  om*/

    CDATASection cdata = child.getOwnerDocument().createCDATASection(value);
    child.appendChild(cdata);
    return child;
}

From source file:Main.java

/**
 * Creates a text node with the given content and appends it as child to the given element.
 * //from   w  w  w  .j a  v a 2 s . c om
 * @param domElement the element to recieve the text node
 * @param textContent the content for the text node
 */
public static void appendTextContent(Element domElement, String textContent) {
    if (textContent == null) {
        return;
    }
    Document parentDocument = domElement.getOwnerDocument();
    Text textNode = parentDocument.createTextNode(textContent);
    domElement.appendChild(textNode);
}

From source file:Main.java

public static Element clear(Element node, String subElement) {
    Element child = findFirst(node, subElement);
    if (child == null) {
        child = node.getOwnerDocument().createElement(subElement);
        node.appendChild(child);/*from www  . ja va  2  s . c o  m*/
    }
    child.setTextContent(null); // remove all descendants
    return child;
}