Example usage for org.w3c.dom Node DOCUMENT_NODE

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

Introduction

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

Prototype

short DOCUMENT_NODE

To view the source code for org.w3c.dom Node DOCUMENT_NODE.

Click Source Link

Document

The node is a Document.

Usage

From source file:Main.java

public static boolean isElement(Node node) {
    short nodeType = node.getNodeType();
    if (Node.ELEMENT_NODE == nodeType || Node.DOCUMENT_NODE == nodeType) {
        return true;
    }/*from w w w  .  j  a  va  2s.c  o m*/
    return false;
}

From source file:Main.java

public static boolean isElement(Node node) {
    return node != null
            && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE);
}

From source file:Main.java

private static DOMImplementation getDocImplementation(Node node) {
    DOMImplementation domImpl;/*from w  w w .  j a  v a2s . c om*/
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        domImpl = ((Document) node).getImplementation();
    } else {
        domImpl = node.getOwnerDocument().getImplementation();
    }
    return domImpl;
}

From source file:Main.java

/**
 * to get node's owner document//  www  .  jav a 2  s. c  o  m
 *
 * @return The document that contain node
 */
public static Document getOwnerDocument(Node node) {
    Document doc = null;
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        doc = (Document) node;
    } else {
        doc = node.getOwnerDocument();
    }
    return doc;
}

From source file:Main.java

/**
 * @param node/*from  w w w. j  a  v a  2 s  .c om*/
 */
public static void displayNodeInfo(Node node) {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node ");
        break;
    case Node.ELEMENT_NODE:
        System.out.println("Element Node: " + node.getNodeName());
        break;
    case Node.TEXT_NODE:
        System.out.println("Text Node: " + node.getNodeName());
        break;
    case Node.CDATA_SECTION_NODE:
        System.out.println("CDATA Section Node: ");
        break;
    case Node.COMMENT_NODE:
        System.out.println("Comment Node ");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Processing Instruction Node ");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Reference Node ");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Type Node ");
        break;
    }
}

From source file:Main.java

public static Node addChildNode(Node pNode, String name, String value) {
    Document _doc = null;/*from  w  w w . java 2  s.  c  o m*/
    if (pNode.getNodeType() == Node.DOCUMENT_NODE) {
        _doc = (Document) pNode;
    } else {
        _doc = pNode.getOwnerDocument();
    }

    Element _ele = _doc.createElement(name);
    Node _node = _doc.createTextNode(value);
    _ele.appendChild(_node);

    return pNode.appendChild(_ele);
}

From source file:Main.java

public static String domNode2String(Node node, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += domNode2String(nodes.item(i), escapeStrings);
            }//from www  .j a  v a2 s  . c o  m
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                ret += domNode2String(children.item(i), escapeStrings);
            }
        }

        ret += "</" + name + ">";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;
    case Node.COMMENT_NODE:
        ret += "<!--" + node.getNodeValue() + "-->";
        break;
    }
    return ret;
}

From source file:Main.java

public static String getNodeHierarchy(Node node) {

    StringBuffer sb = new StringBuffer();
    if (node != null) {
        Stack<Node> st = new Stack<Node>();
        st.push(node);/*from  w  w  w.  j  a v  a  2  s . c om*/

        Node parent = node.getParentNode();

        while ((parent != null) && (parent.getNodeType() != Node.DOCUMENT_NODE)) {

            st.push(parent);

            parent = parent.getParentNode();
        }

        // constructs node hierarchy
        Node n = null;
        while (!st.isEmpty() && null != (n = st.pop())) {

            if (n instanceof Element) {
                Element e = (Element) n;

                if (sb.length() == 0) {
                    sb.append(e.getTagName());
                } else {
                    sb.append("/" + e.getTagName());
                }
            }

        }
    }
    return sb.toString();

}

From source file:Main.java

/**prints the document tree
 * @param node node to start at/*from  ww  w . ja va  2  s.  c om*/
 * @param ident amount of indention*/
public static void printTree(Node node, int ident) {
    if (node == null)
        return;
    NodeList children;
    System.out.print("Node: " + node.getNodeName() + " ");
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        System.out.println("Document Node");
        break;

    case Node.ELEMENT_NODE:
        System.out.println("Element Node");
        break;

    case Node.TEXT_NODE:
        System.out.println("->" + node.getNodeValue().trim() + "<-");
        break;

    case Node.CDATA_SECTION_NODE:
        System.out.println("CData Node");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println("Proposing Instruction Node");
        break;

    case Node.ENTITY_REFERENCE_NODE:
        System.out.println("Entity Node");
        break;

    case Node.DOCUMENT_TYPE_NODE:
        System.out.println("Document Node");
        break;

    default:
    }

    for (int j = 0; j < 2 * ident; j++)
        System.out.print(" ");
    System.out.println("It has the following Children");
    children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            for (int j = 0; j < ident; j++)
                System.out.print(" ");
            System.out.print("Child " + ident + "." + i + " = ");
            printNodeType(children.item(i), ident + 1);
        }
        System.out.println();
    }
}

From source file:Main.java

private static String prettyPrintDom(Node node, String indent, boolean isRoot, boolean escapeStrings) {
    String ret = "";
    switch (node.getNodeType()) {

    case Node.DOCUMENT_NODE:
        // recurse on each child
        NodeList nodes = node.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                ret += prettyPrintDom(nodes.item(i), indent, isRoot, escapeStrings);
            }/*from   w ww  .  jav a 2  s  . com*/
        }
        break;

    case Node.ELEMENT_NODE:
        String name = node.getNodeName();
        ret += indent + "<" + name;
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node current = attributes.item(i);
            ret += " " + current.getNodeName() + "=\""
                    + ((escapeStrings) ? escapeStringForXML(current.getNodeValue()) : current.getNodeValue())
                    + "\"";
        }
        ret += ">";

        // recurse on each child
        NodeList children = node.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                String tmp = prettyPrintDom(children.item(i), indent + ((isRoot) ? "" : BI), false,
                        escapeStrings);
                if (!tmp.replaceAll("[\\s]+", "").equals(""))
                    if (tmp.endsWith("\n"))
                        if (ret.endsWith("\n"))
                            ret += tmp;
                        else
                            ret += "\n" + tmp;
                    else
                        ret += tmp;
            }
        }
        if (ret.endsWith("\n"))
            ret += indent + "</" + name + ">\n";
        else
            ret += "</" + name + ">\n";
        break;

    case Node.TEXT_NODE:
        ret += (escapeStrings) ? escapeStringForXML(node.getNodeValue()) : node.getNodeValue();
        break;

    case Node.COMMENT_NODE:
        ret += "<!-- " + node.getNodeValue() + " -->";
        break;
    }
    return ret;
}