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

/** Prints the specified node, then prints all of its children. */

public static void printDOM(Node node) {

    int type = node.getNodeType();

    switch (type) {

    // print the document element
    case Node.DOCUMENT_NODE: {
        System.out.print("<?xml version=\"1.0\" ?>");
        printDOM(((Document) node).getDocumentElement());
        break;// w  w  w.jav  a  2 s .  c  om
    }

    // print element with attributes
    case Node.ELEMENT_NODE: {
        System.out.println();
        System.out.print("<");
        System.out.print(node.getNodeName());
        NamedNodeMap attrs = node.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node attr = attrs.item(i);
            System.out.print(" " + attr.getNodeName().trim() + "=\"" + attr.getNodeValue().trim() + "\"");
        }
        System.out.print(">");
        NodeList children = node.getChildNodes();

        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++)
                printDOM(children.item(i));
        }
        break;
    }

    // handle entity reference nodes

    case Node.ENTITY_REFERENCE_NODE: {
        System.out.print("&");
        System.out.print(node.getNodeName().trim());
        System.out.print(";");
        break;
    }

    // print cdata sections
    case Node.CDATA_SECTION_NODE: {
        System.out.print("<![CDATA[");
        System.out.print(node.getNodeValue().trim());
        System.out.print("]]>");
        break;
    }

    // print text
    case Node.TEXT_NODE: {
        System.out.println();
        System.out.print(node.getNodeValue().trim());
        break;
    }

    // print processing instruction

    case Node.PROCESSING_INSTRUCTION_NODE: {
        System.out.print("<?");
        System.out.print(node.getNodeName().trim());
        String data = node.getNodeValue().trim();
        {
            System.out.print(" ");
            System.out.print(data);
        }
        System.out.print("?>");
        break;
    }
    }

    if (type == Node.ELEMENT_NODE) {
        System.out.println();
        System.out.print("</");
        System.out.print(node.getNodeName().trim());
        System.out.print('>');
    }
}

From source file:MainClass.java

public static void print(Node node, OutputStream os) {
    PrintStream ps = new PrintStream(os);
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
        ps.print("<" + node.getNodeName());

        NamedNodeMap map = node.getAttributes();
        for (int i = 0; i < map.getLength(); i++) {
            ps.print(" " + map.item(i).getNodeName() + "=\"" + map.item(i).getNodeValue() + "\"");
        }//  www  . j  a  va 2  s .co  m
        ps.println(">");
        return;
    case Node.ATTRIBUTE_NODE:
        ps.println(node.getNodeName() + "=\"" + node.getNodeValue() + "\"");
        return;
    case Node.TEXT_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.CDATA_SECTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ps.println(node.getNodeValue());
        return;
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        ps.println(node.getNodeName() + "=" + node.getNodeValue());
        return;
    }
}

From source file:DOMHelper.java

/**
 * Gets the owner document of a node.//w w  w.j  av a  2s  . c o  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

/**
 * Constructs a XPath query to the supplied node.
 * //from  ww w .  ja  va 2 s .  co  m
 * @param n
 * @return
 */
public static String getXPath(Node n) {
    if (null == n) {
        throw new IllegalArgumentException("Invalid node");
    }

    ArrayList<Node> hierarchy = new ArrayList<Node>();
    StringBuffer buffer = new StringBuffer();
    Node parent = null;

    // Push parent element's on stack
    hierarchy.add(n);
    parent = n.getParentNode();
    while (parent != null && parent.getNodeType() != Node.DOCUMENT_NODE) {
        hierarchy.add(0, parent);
        parent = parent.getParentNode();
    }

    Iterator<Node> i = hierarchy.iterator();
    while (i.hasNext()) {
        Node node = i.next();
        buffer.append("/");
        buffer.append(node.getNodeName());
        if (node.hasAttributes()) {
            Node uuid = node.getAttributes().getNamedItem("uuid");
            if (uuid != null) {
                buffer.append("[@uuid='");
                buffer.append(uuid.getNodeValue());
                buffer.append("']");
            }
        }
    }

    // return buffer
    return buffer.toString();
}

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  .  c  o  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

private static void prettyPrintLoop(Node node, StringBuilder string, String indentation) {
    if (node == null) {
        return;//from   w ww .  j  a  v  a  2 s .  c o  m
    }

    int type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE:
        string.append("\n");
        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");
        break;

    case Node.ELEMENT_NODE:
        string.append(indentation);
        string.append("<");
        string.append(node.getNodeName());

        Attr[] attributes;
        if (node.getAttributes() != null) {
            int length = node.getAttributes().getLength();
            attributes = new Attr[length];
            for (int loopIndex = 0; loopIndex < length; loopIndex++) {
                attributes[loopIndex] = (Attr) node.getAttributes().item(loopIndex);
            }
        } else {
            attributes = new Attr[0];
        }

        for (Attr attribute : attributes) {
            string.append(" ");
            string.append(attribute.getNodeName());
            string.append("=\"");
            string.append(attribute.getNodeValue());
            string.append("\"");
        }

        string.append(">\n");

        prettyPrintLoop(node.getChildNodes(), string, indentation + "\t");

        string.append(indentation);
        string.append("</");
        string.append(node.getNodeName());
        string.append(">\n");

        break;

    case Node.TEXT_NODE:
        string.append(indentation);
        string.append(node.getNodeValue().trim());
        string.append("\n");
        break;

    case Node.PROCESSING_INSTRUCTION_NODE:
        string.append(indentation);
        string.append("<?");
        string.append(node.getNodeName());
        String text = node.getNodeValue();
        if (text != null && text.length() > 0) {
            string.append(text);
        }
        string.append("?>\n");
        break;

    case Node.CDATA_SECTION_NODE:
        string.append(indentation);
        string.append("<![CDATA[");
        string.append(node.getNodeValue());
        string.append("]]>");
        break;
    }
}

From source file:Main.java

/**
 * returns the Node Type As String//from  www  .ja  v a  2  s  .c o  m
 * @param node
 * @param cftype 
 * @return
 */
public static String getTypeAsString(Node node, boolean cftype) {
    String suffix = cftype ? "" : "_NODE";

    switch (node.getNodeType()) {
    case Node.ATTRIBUTE_NODE:
        return "ATTRIBUTE" + suffix;
    case Node.CDATA_SECTION_NODE:
        return "CDATA_SECTION" + suffix;
    case Node.COMMENT_NODE:
        return "COMMENT" + suffix;
    case Node.DOCUMENT_FRAGMENT_NODE:
        return "DOCUMENT_FRAGMENT" + suffix;
    case Node.DOCUMENT_NODE:
        return "DOCUMENT" + suffix;
    case Node.DOCUMENT_TYPE_NODE:
        return "DOCUMENT_TYPE" + suffix;
    case Node.ELEMENT_NODE:
        return "ELEMENT" + suffix;
    case Node.ENTITY_NODE:
        return "ENTITY" + suffix;
    case Node.ENTITY_REFERENCE_NODE:
        return "ENTITY_REFERENCE" + suffix;
    case Node.NOTATION_NODE:
        return "NOTATION" + suffix;
    case Node.PROCESSING_INSTRUCTION_NODE:
        return "PROCESSING_INSTRUCTION" + suffix;
    case Node.TEXT_NODE:
        return "TEXT" + suffix;
    default:
        return "UNKNOW" + suffix;
    }
}

From source file:Main.java

/**
 * based on public Java5 javadoc of org.w3c.dom.Node.getTextContent method
 *//*  w w  w  .ja  v  a 2  s. c  o m*/
public static String getTextContent(Node node) {
    switch (node.getNodeType()) {
    case Node.ELEMENT_NODE:
    case Node.ATTRIBUTE_NODE:
    case Node.ENTITY_NODE:
    case Node.ENTITY_REFERENCE_NODE:
    case Node.DOCUMENT_FRAGMENT_NODE:
        return mergeTextContent(node.getChildNodes());
    case Node.TEXT_NODE:
    case Node.CDATA_SECTION_NODE:
    case Node.COMMENT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return node.getNodeValue();
    case Node.DOCUMENT_NODE:
    case Node.DOCUMENT_TYPE_NODE:
    case Node.NOTATION_NODE:
    default:
        return null;
    }
}

From source file:Main.java

private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude,
        final boolean com) {
    if (rootNode == exclude) {
        return;//w  w  w  . j av  a  2  s .co  m
    }
    switch (rootNode.getNodeType()) {
    case Node.ELEMENT_NODE:
        result.add(rootNode);
        Element el = (Element) rootNode;
        if (el.hasAttributes()) {
            NamedNodeMap nl = el.getAttributes();
            for (int i = 0; i < nl.getLength(); i++) {
                result.add(nl.item(i));
            }
        }
        //no return keep working
    case Node.DOCUMENT_NODE:
        for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
            if (r.getNodeType() == Node.TEXT_NODE) {
                result.add(r);
                while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
                    r = r.getNextSibling();
                }
                if (r == null) {
                    return;
                }
            }
            getSetRec(r, result, exclude, com);
        }
        return;
    case Node.COMMENT_NODE:
        if (com) {
            result.add(rootNode);
        }
        return;
    case Node.DOCUMENT_TYPE_NODE:
        return;
    default:
        result.add(rootNode);
    }
}

From source file:Main.java

/**
 * Convert a node type to a string. For debug purpose only.
 * /*from ww w  .j av  a 2 s .  c o  m*/
 * @param nodeType
 *            the node type
 * @return the string
 * @throws Exception
 *             the exception
 */
public static String nodeTypeToString(short nodeType) throws Exception {
    if (nodeType == Node.ELEMENT_NODE)
        return "ELEMENT_NODE";
    if (nodeType == Node.ATTRIBUTE_NODE)
        return "ATTRIBUTE_NODE";
    if (nodeType == Node.TEXT_NODE)
        return "TEXT_NODE";
    if (nodeType == Node.CDATA_SECTION_NODE)
        return "CDATA_SECTION_NODE";
    if (nodeType == Node.ENTITY_REFERENCE_NODE)
        return "ENTITY_REFERENCE_NODE";
    if (nodeType == Node.ENTITY_NODE)
        return "ENTITY_NODE";
    if (nodeType == Node.PROCESSING_INSTRUCTION_NODE)
        return "PROCESSING_INSTRUCTION_NODE";
    if (nodeType == Node.COMMENT_NODE)
        return "COMMENT_NODE";
    if (nodeType == Node.DOCUMENT_NODE)
        return "DOCUMENT_NODE";
    if (nodeType == Node.DOCUMENT_TYPE_NODE)
        return "DOCUMENT_TYPE_NODE";
    if (nodeType == Node.DOCUMENT_FRAGMENT_NODE)
        return "DOCUMENT_FRAGMENT_NODE";
    if (nodeType == Node.NOTATION_NODE)
        return "NOTATION_NODE";
    if (nodeType == Node.DOCUMENT_POSITION_DISCONNECTED)
        return "DOCUMENT_POSITION_DISCONNECTED";
    if (nodeType == Node.DOCUMENT_POSITION_PRECEDING)
        return "DOCUMENT_POSITION_PRECEDING";
    if (nodeType == Node.DOCUMENT_POSITION_FOLLOWING)
        return "DOCUMENT_POSITION_FOLLOWING";
    if (nodeType == Node.DOCUMENT_POSITION_CONTAINS)
        return "DOCUMENT_POSITION_CONTAINS";
    if (nodeType == Node.DOCUMENT_POSITION_CONTAINED_BY)
        return "DOCUMENT_POSITION_CONTAINED_BY";
    if (nodeType == Node.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC)
        return "DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC";

    throw new Exception("Unknown value : " + nodeType);
}