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

public static String convertToString(Node node) {
    boolean withXMLDeclaration = true;
    String result;/* w  ww.j  a va2 s.  c  o m*/
    if (withXMLDeclaration) {
        Document document = node.getOwnerDocument();
        DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
        LSSerializer serializer = domImplLS.createLSSerializer();
        result = serializer.writeToString(node);
    } else {
        try {
            TransformerFactory transFactory = TransformerFactory.newInstance();
            Transformer transformer = transFactory.newTransformer();
            StringWriter buffer = new StringWriter();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.transform(new DOMSource(node), new StreamResult(buffer));
            result = buffer.toString();
        } catch (TransformerConfigurationException e) {
            result = "";
        } catch (TransformerException e) {
            result = "";
        }
    }
    return result;
}

From source file:Main.java

/**
 * Searches below the supplied Node for a "list" tag and then retrieves the contents of the "value" tag(s) under that.
 * Note that if there is more than one "list" under the node then we will take the values from the first one only.
 * //from   w w  w .  j  av  a  2 s .com
 * @param node
 *          acts as the root for the seach
 * 
 * @return list of Strings corresponding to the contents of the "value" tag(s)
 */
public static ArrayList getElementListValues(Node node) {
    ArrayList values = new ArrayList();

    // search for list tag
    Document doc = node.getOwnerDocument();
    NodeList list = doc.getElementsByTagName("list");

    if (list.getLength() == 0)
        return values;

    // search under that for value tag(s)
    doc = list.item(0).getOwnerDocument();
    NodeList vals = doc.getElementsByTagName("value");

    // for each one we get the text contents
    for (int i = 0; i < vals.getLength(); i++) {
        Node v = vals.item(i);
        NodeList text = v.getChildNodes();

        if (text == null) {
            values.add("");
            continue;
        }

        // there should be only text inside the value tag
        Node value = text.item(0);
        if (value == null)
            values.add("");
        else
            values.add(value.getNodeValue());
    }

    return values;
}

From source file:Main.java

private static DOMImplementation getDocImplementation(Node node) {
    DOMImplementation domImpl;/*from  www .j a v a  2  s . co  m*/
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        domImpl = ((Document) node).getImplementation();
    } else {
        domImpl = node.getOwnerDocument().getImplementation();
    }
    return domImpl;
}

From source file:Main.java

public static String getStartDateFromLineNode(Node linenode) {
    String date = "";
    Node t1 = linenode.getAttributes().getNamedItem("startdate");
    if (t1 != null)
        date = t1.getNodeValue();/*from   ww  w.j  a  v  a2s.c  o  m*/
    else
        date = linenode.getOwnerDocument().getElementsByTagName("startdate").item(0).getFirstChild()
                .getNodeValue();
    return date;
}

From source file:Main.java

/**
 * This method returns the owner document of a particular node.
 * This method is necessary because it <I>always</I> returns a
 * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
 * if the {@link Node} is a {@link Document}.
 *
 * @param node//from   w w w . j a v  a 2s.co m
 * @return the owner document of the node
 */
public static Document getOwnerDocument(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return (Document) node;
    }
    try {
        return node.getOwnerDocument();
    } catch (NullPointerException npe) {
        throw new NullPointerException(npe.getMessage());
    }
}

From source file:Main.java

@SuppressWarnings("null")
public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;// w  w  w  . jav  a 2 s  .  c o m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                /*
                 if (domimpl && !attr.getSpecified()) {
                 ((Attr) element.getAttributeNode(attrName)).setSpecified(false);
                 }
                 */
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + node.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null && dest != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

public static Element createElement(Node parent, String tagName) {
    Document doc;//from  w  w w  .j  a  v  a2 s . c  om
    if (parent instanceof Document) {
        doc = (Document) parent;
    } else {
        doc = parent.getOwnerDocument();
    }
    Element e = doc.createElement(tagName);
    parent.appendChild(e);
    return e;
}

From source file:Main.java

public static CDATASection addCDataText(Node node, String data) {
    Document doc = null;//  w  ww.  j  av  a 2s.  co m
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    CDATASection e = doc.createCDATASection(data);
    node.appendChild(e);
    return e;
}

From source file:Main.java

public static void copyInto(Node src, Node dest) throws DOMException {

    Document factory = dest.getOwnerDocument();

    //Node start = src;
    Node parent = null;//from www .j av a 2  s.co  m
    Node place = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr) attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(), place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException(
                    "can't copy node type, " + type + " (" + place.getNodeName() + ')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place = place.getFirstChild();
            dest = node;
        } else if (parent == null) {
            place = null;
        } else {
            // advance
            place = place.getNextSibling();
            while (place == null && parent != null) {
                place = parent.getNextSibling();
                parent = parent.getParentNode();
                dest = dest.getParentNode();
            }
        }

    }

}

From source file:Main.java

public static Comment addComment(Node node, String comment) {
    Document doc = null;//from w w  w . ja  va  2  s  .c om
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    Comment e = doc.createComment(comment);
    node.appendChild(e);
    return e;
}