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 Element newElement(Element elParent, String tagName, String text) {
    Element element = newElement(elParent, tagName);
    if (null != element) {
        Text tx = element.getOwnerDocument().createTextNode(text);
        element.appendChild(tx);/*w ww.  ja  va2  s . com*/
    }
    return element;
}

From source file:Main.java

/**
 * Adds an {@link Element} child to the given element.
 *
 * @param parent The {@link Element} to add the {@link Element} child to.
 * @param qualifiedName The fully qualified name of the new {@link Element}
 * child./*ww w . j av  a2 s  .c om*/
 * @return Returns the new {@link Element} child.
 */
public static Element addElementChildTo(Element parent, String nsURI, String qualifiedName) {
    final Document doc = parent.getOwnerDocument();
    final Element child = doc.createElementNS(nsURI, qualifiedName);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

public static Element appendResultSetToNode(Element root, String rowTag, ResultSet rs) throws SQLException {
    Document doc = root.getOwnerDocument();

    ResultSetMetaData meta = rs.getMetaData();
    int columnCount = meta.getColumnCount();
    int rowCount = 0;
    while (rs.next()) {
        Element rowElement = doc.createElement(rowTag);
        rowElement.setAttribute("row", "" + rowCount);
        for (int i = 1; i <= columnCount; i++) {
            rowElement.setAttribute(meta.getColumnName(i), rs.getString(i));
        }/*from  w w w .j  a  v a  2  s.c o m*/
        rowCount++;
        root.appendChild(rowElement);
    }

    return root;
}

From source file:Utils.java

/**
 * Add a new element to the given parent
 * @param parent the parent Element// ww  w  .  j a  v a 2  s . co m
 * @param name the child name
 * @return new Element
 */
public static Element createElement(Element parent, String name) {
    Document document;
    Element element;

    document = parent.getOwnerDocument();
    element = document.createElement(name);

    parent.appendChild(element);
    return element;
}

From source file:Main.java

/**
 * create an child element with the specified name and value and append it in a parent element
 *
 * @param parent/*from  w w  w  . j  a v a2s.co  m*/
 * @param tagName
 * @param value
 * @return
 */
public static Element createElement(Element parent, String tagName, String value) {
    Document doc = parent.getOwnerDocument();
    Element child = doc.createElement(tagName);
    setElementValue(child, value);
    parent.appendChild(child);
    return child;
}

From source file:Main.java

/**
 * Creates a DOM text element//  w w w  .  j ava2  s  .co m
 * @param parent parent DOM element
 * @param tagName element name
 * @param text element text
 * @return a DOM element
 */
public static Element createTextElement2(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element e = doc.createElement(tagName);
    e.appendChild(doc.createTextNode(text));
    parent.appendChild(e);
    return e;
}

From source file:Main.java

/**
 * @param target/*from w ww .  j  a  v  a2s .c o m*/
 * @param node
 * @return
 */
public static Node copyNode(Element target, Node node) {
    Node n = node.cloneNode(true);
    Document targetDoc = target.getOwnerDocument();
    targetDoc.adoptNode(n);
    target.appendChild(n);
    return node;
}

From source file:Main.java

public static Element createTextElement(Element parent, String tagName, String text) {
    Document doc = parent.getOwnerDocument();
    Element e = createElement(parent, tagName);
    if (text == null) {
        text = "";
    }/* w w  w .ja  v  a 2  s  .  co  m*/
    e.appendChild(doc.createTextNode(text));
    return e;
}

From source file:Main.java

public static void setElementTextValue(Element e, String text, boolean cdata) {
    Document root = e.getOwnerDocument();
    e.normalize();//from w  ww. j  a v a2 s .c  om
    if (e.hasChildNodes()) {
        NodeList nl = e.getChildNodes();

        /* This suxx: NodeList Object is changed when removing children !!!
           I will store all nodes that should be deleted in a Vector and delete them afterwards */
        int length = nl.getLength();

        List<Node> v = new ArrayList<Node>(nl.getLength());
        for (int i = 0; i < length; i++)
            if (nl.item(i) instanceof CharacterData)
                v.add(nl.item(i));
        for (Node n : v)
            e.removeChild(n);
    }

    if (cdata) {
        e.appendChild(root.createCDATASection(text));
    } else {
        e.appendChild(root.createTextNode(text));
    }
}

From source file:Main.java

public static String getXPath(Element elt) {
    String xpath = "";
    if (elt != null) {
        Document doc = elt.getOwnerDocument();
        Element root = doc.getDocumentElement();
        Element parent = elt;//w w w  . j a v a  2s.  co m
        while (parent != root) {
            xpath = "/" + parent.getNodeName() + xpath;
            parent = (Element) parent.getParentNode();
        }
    }
    return xpath;
}