Example usage for org.w3c.dom Node getOwnerDocument

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

Introduction

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

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:Main.java

private static Document getDoc(Node n) {
    if (n instanceof Document)
        return (Document) n;
    else/*from  w  w w . j a v  a2 s .  co m*/
        return n.getOwnerDocument();
}

From source file:Main.java

public static void nodeSubDelete(Element root, String table, String queryType) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    NodeList nl = find.getChildNodes();
    int numnodes = nl.getLength();

    System.out.println("length : " + numnodes);

    Node deleteNode = null;/*from   ww w . ja  va2s .co  m*/

    for (int i = 0; i < numnodes; i++) {
        Node n = nl.item(i);
        String name = n.getNodeName();

        if (name.equals(queryType)) {
            deleteNode = n;
            System.out.println("Finding Node : " + deleteNode.getNodeName());

            break;
        }
    }

    find.removeChild(deleteNode);

    print(doc);
}

From source file:Main.java

/**
 * Get the owner document of the passed node. If the node itself is a
 * document, only a cast is performed.//from   www . j a v  a 2 s . c o m
 *
 * @param aNode
 *        The node to get the document from. May be <code>null</code>.
 * @return <code>null</code> if the passed node was <code>null</code>.
 */
@Nullable
public static Document getOwnerDocument(@Nullable final Node aNode) {
    return aNode == null ? null : aNode instanceof Document ? (Document) aNode : aNode.getOwnerDocument();
}

From source file:Main.java

public static Node addAttribute(Node pNode, String attrName, String attrValue) {
    Node attributeNode = null;/*from  ww w.j ava  2 s  . c om*/
    try {
        Attr _attr = pNode.getOwnerDocument().createAttribute(attrName);
        _attr.setNodeValue(attrValue);

        attributeNode = pNode.getAttributes().setNamedItem(_attr);

    } catch (Exception e) {
        attributeNode = null;
    }

    return attributeNode;
}

From source file:Main.java

public static Document getDocument(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return (Document) node;
    }// w ww . j  a  v a 2 s .  c  o m
    return node.getOwnerDocument();
}

From source file:Main.java

public static void nodeSubUpdate(Element root, String table, String queryType, String queryValue) {
    Node find = getTag(root, table);
    Document doc = find.getOwnerDocument();

    NodeList nl = find.getChildNodes();
    int numnodes = nl.getLength();

    System.out.println("length : " + numnodes);

    Node replNode = null;//  w w  w .  j  a  va 2s. c om

    for (int i = 0; i < numnodes; i++) {
        Node n = nl.item(i);
        String name = n.getNodeName();

        if (name.equals(queryType)) {
            replNode = n;
            System.out.println("Finding Node : " + replNode.getNodeName());

            break;
        }
    }

    Element type = doc.createElement(queryType);
    Text value = doc.createTextNode(queryValue);

    type.appendChild(value);

    find.replaceChild(type, replNode);

    print(doc);
}

From source file:Main.java

static public String getInnerXmlText(Node xmlNode) {
    StringBuilder result = new StringBuilder();

    Document xmlDocument = xmlNode.getOwnerDocument();
    DOMImplementation xmlDocumentImpl = xmlDocument.getImplementation();
    DOMImplementationLS lsImpl = (DOMImplementationLS) xmlDocumentImpl.getFeature("LS", "3.0");
    LSSerializer lsSerializer = lsImpl.createLSSerializer();

    NodeList childNodes = xmlNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        String childText = lsSerializer.writeToString(childNodes.item(i));
        int pos = childText.indexOf("?>");
        if (pos > -1) {
            childText = childText.substring(pos + 2);
        }//from   w  w  w . j  a va  2 s.  c  o  m
        result.append(childText);
    }

    return result.toString();
}

From source file:DOMHelper.java

/**
 * Gets the owner document of a node.//  ww w.  j  a  v  a2s.co m
 *
 * @param node the node
 * @return the node's document or the node itself if it is a document node
 *
 * @throws NullPointerException if {@code node} is {@code null}
 */
public static Document getOwnerDocument(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE)
        return (Document) node;
    return node.getOwnerDocument();
}

From source file:Main.java

public static void addNewElementWithAttribute(Node node, String elementName, String elementValue,
        String attrName, String attrValue) {
    Document xmlDoc = node.getOwnerDocument();
    addNewElementWithAttribute(xmlDoc, node, elementName, elementValue, attrName, attrValue);
}

From source file:Main.java

public static void setXsdSchema(Node node, String schemaURL) {
    Document doc;/*from  ww  w .  j  a  v a 2  s.  c o  m*/
    if (node.getNodeType() != Node.DOCUMENT_NODE) {
        doc = node.getOwnerDocument();
    } else {
        doc = (Document) node;
    }
    Element root = doc.getDocumentElement();
    if (schemaURL == null) {
        root.removeAttribute("xmlns:xsi");
        root.removeAttribute("xsi:noNamespaceSchemaLocation");
    } else {
        root.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        root.setAttribute("xsi:noNamespaceSchemaLocation", schemaURL);
    }
}