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

/**
 * Sets element TEXT data/*from  w w w. j a v a  2 s.  c o m*/
 * 
 * @param e
 *            the element
 * @param data
 *            the new data
 */
public static void setElementTextValue(Element e, String data) {
    Text txt = getElementTextNode(e);
    if (txt != null) {
        txt.setData(data);
    } else {
        txt = e.getOwnerDocument().createTextNode(data);
        e.appendChild(txt);
    }
}

From source file:Main.java

/**
 * Appends the CDATA element to the parent element.
 * /*from w ww  . j  a v  a2s .  com*/
 * @param parent the parent element
 * @param tagName the CDATA element name
 * @param value the CDATA element value
 * @return the CDATA element added to the parent element
 */
public static Element appendCDATAElement(Element parent, String tagName, String value) {
    Element child = appendElement(parent, tagName);
    if (value == null) { // avoid "null" word in the XML payload
        value = "";
    }

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

From source file:Main.java

static public void setElementText(Element elm, String text) {
    Node node = elm.getFirstChild();
    if (node == null) {
        if (text != null)
            elm.appendChild(elm.getOwnerDocument().createTextNode(text));
    } else if (node.getNodeType() == Node.TEXT_NODE) {
        if (text == null)
            node.getParentNode().removeChild(node);
        else//from  w  ww .j  a v a  2 s  .c  om
            node.setNodeValue(text);
    } else if (text != null) {
        Text textNode = node.getOwnerDocument().createTextNode(text);
        elm.insertBefore(textNode, elm.getFirstChild());
    }
}

From source file:Main.java

/** 
 * Appends the CDATA element to the parent element. 
 * /*from  www .  ja v  a 2 s. co  m*/
 * @param parent the parent element 
 * @param tagName the CDATA element name 
 * @param value the CDATA element value 
 * @return the CDATA element added to the parent element 
 */
public static Element appendCDATAElement(Element parent, String tagName, String value) {
    Element child = appendElement(parent, tagName);
    if (value == null) { // avoid "null" word in the XML payload  
        value = "";
    }

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

From source file:Main.java

/**
 * Sets element CDATA data/*from   w w  w  . jav  a 2 s.  c om*/
 * 
 * @param e
 *            the lement
 * @param data
 *            the new data
 */
public static void setElementCDataValue(Element e, String data) {
    CDATASection txt = getElementCDataNode(e);
    if (txt != null) {
        txt.setData(data);
    } else {
        txt = e.getOwnerDocument().createCDATASection(data);
        e.appendChild(txt);
    }
}

From source file:Main.java

public static void appendStringElement(Element parentElement, String nodeName, String nodeValue) {
    // check for exists
    Element elem = selectSingleElement(parentElement, nodeName);

    if (elem == null) {
        elem = parentElement.getOwnerDocument().createElement(nodeName);
        parentElement.appendChild(elem);
    }/*from w w  w  .  ja va2s. com*/

    elem.setTextContent(nodeValue);
}

From source file:Main.java

public static void setChildElementText(Element element, String name, String text) {
    Element elm = getFirstChildElement(element, name);
    if (elm == null) {
        elm = element.getOwnerDocument().createElement(name);
        element.appendChild(elm);/*from w ww  .  java2 s .  c  o  m*/
    }

    setElementText(elm, text);
}

From source file:Main.java

private static void transferAttributes(Element elm, Element elm2) {
    NamedNodeMap attributes = elm.getAttributes();
    for (int c = 0; c < attributes.getLength(); c++) {
        Attr attr = (Attr) attributes.item(c);
        elm2.setAttributeNodeNS((Attr) elm2.getOwnerDocument().importNode(attr, true));
    }/*www  . j a v a 2s.c  o m*/
}

From source file:com.evolveum.midpoint.cli.common.ToolsUtils.java

public static void setNamespaceDeclaration(Element element, String prefix, String namespaceUri) {
    Document doc = element.getOwnerDocument();
    NamedNodeMap attributes = element.getAttributes();
    Attr attr;/*w  ww .  j  a  v  a2  s. c  o  m*/
    if (prefix == null || prefix.isEmpty()) {
        // default namespace
        attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, XMLConstants.XMLNS_ATTRIBUTE);
    } else {
        attr = doc.createAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
                XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix);
    }
    checkValidXmlChars(namespaceUri);
    attr.setValue(namespaceUri);
    attributes.setNamedItem(attr);
}

From source file:Main.java

public static void appendElementText(Element parentElement, String elementName, String elementValue) {

    if (parentElement == null || elementName == null || elementValue == null)
        throw new NullPointerException();

    Element childElement = parentElement.getOwnerDocument().createElement(elementName);
    setTextContent(childElement, elementValue);
    parentElement.appendChild(childElement);
}