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:eu.dasish.annotation.backend.Helpers.java

public static String elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String result = serializer.writeToString(element);
    return result;
}

From source file:Main.java

public static String setChildText(Element elt, String name, String value) {
    Element child = getChildElement(elt, name);

    if (child == null) {
        child = elt.getOwnerDocument().createElement(name);
        elt.appendChild(child);/*ww w  . jav  a2s. c o  m*/
    }
    return setInnerText(child, value);

}

From source file:Main.java

public static void setText(Element ele, String stext) {
    try {// www.ja va  2s.  c o  m
        if (null == ele)
            return;
        if (null == stext) {
            ele.appendChild(ele.getOwnerDocument().createTextNode(""));
        } else {
            ele.getFirstChild().setNodeValue(stext);
        }
    } catch (Exception e) {
        ele.appendChild(ele.getOwnerDocument().createTextNode(stext));
    }
}

From source file:Main.java

/**
 * Find child element and create if not exists
 * @param element parent element/*from w ww  .j  a  v  a2 s .c  o  m*/
 * @param childName child name
 * @return 
 */
public static Element getChildElement(Element element, String childName) {
    Element result = findChildElement(element, childName);
    if (element == null) {
        result = element.getOwnerDocument().createElement(childName);
        element.appendChild(result);
    }
    return result;
}

From source file:Main.java

/**
 * Rename an element, replacing it in its document.
 * @param element the element to rename.
 * @param name the new element name./*from ww  w .j a  v a  2  s.c o m*/
 * @return the renamed element.
 */
public static Element renameElement(Element element, String name) {
    if (element.getNodeName().equals(name))
        return element;
    Element el = element.getOwnerDocument().createElement(name);

    //Copy the attributes
    NamedNodeMap attributes = element.getAttributes();
    int nAttrs = attributes.getLength();
    for (int i = 0; i < nAttrs; i++) {
        Node attr = attributes.item(i);
        el.setAttribute(attr.getNodeName(), attr.getNodeValue());
    }

    //Copy the children
    Node node = element.getFirstChild();
    while (node != null) {
        Node clone = node.cloneNode(true);
        el.appendChild(clone);
        node = node.getNextSibling();
    }

    //Replace the element
    element.getParentNode().replaceChild(el, element);
    return el;
}

From source file:Main.java

public static Element createElement(Element parent, String path) {
    int i = path.indexOf('.');
    Element element = null;//w  ww . jav a2 s .c o  m
    if (i < 0) {
        element = parent.getOwnerDocument().createElement(path);
        parent.appendChild(element);
    } else {
        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);
        }
        element = createElement(pe, c);
    }
    return element;
}

From source file:Main.java

/**
 * This method writes the XML into a nicely formatted string.
 * /*  w  ww  .  j  ava2s.  c o  m*/
 * @param eRoot The root element.
 * @param osOutput The stream to write the data to.
 */
public static void writePretty(Element eRoot, OutputStream osOutput) {
    Object of;

    try {
        of = s_cOutputFormat.newInstance(new Object[] { eRoot.getOwnerDocument() });

        s_mOFSetIndent.invoke(of, new Object[] { 4 });
        s_mOFSetIndenting.invoke(of, new Object[] { true });

        Object xs = s_cXMLSerializer.newInstance(new Object[] { osOutput, of });

        s_mXSSerialize.invoke(xs, new Object[] { eRoot });
    } catch (Exception e) {
        throw new RuntimeException("Error writing the XML to a pretty format", e);
    }
}

From source file:Main.java

/**
 * Adds a new element containing a CDATA section to the parent element
 * @param parent the parent element to add the new element to
 * @param namespaceURI the namespace of the added element's tag name, or null if there isn't any..
 * @param tag the tag name of the new element
 * @param text the text of the CDATA section (if text is null or empty no CDATA section will be added).
 *///ww  w.j  av  a 2s .c  o  m
public static void addTextElementNS(Element parent, String namespaceURI, String tag, String text) {

    // Add an element with the given tag name.
    Document document = parent.getOwnerDocument();
    Element textElement = namespaceURI != null ? document.createElementNS(namespaceURI, tag)
            : document.createElement(tag);
    parent.appendChild(textElement);

    if (text != null && text.length() > 0) {
        CDATASection cdata = createCDATASection(document, text);
        textElement.appendChild(cdata);
    }
}

From source file:Main.java

/**
 * Changes the tag name of an element.//from www. j av a 2s .com
 * 
 * @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

/**
 * Converts a dom element to a String/*from  w w w.  j av  a  2  s  . co m*/
 * 
 * @param node
 * @return the dom as a String
 */
public static String writeDomToString(Element node) {
    DOMImplementation domImplementation = node.getOwnerDocument().getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

        LSOutput lsOutput = domImplementationLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");

        StringWriter stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(node, lsOutput);
        return stringWriter.toString();
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}