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:DocWriter.java

private static void serializeNode(Node node, Writer out, String indent) throws IOException {
    switch (node.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.write("<?xml version=\"1.0\"?>" + '\n');
        writeNode(((Document) node).getDocumentElement(), out, indent);
        break;//from   www. j  av a2s  . co m
    case Node.ELEMENT_NODE:
        writeNode(node, out, indent);
        break;
    case Node.TEXT_NODE:
        out.write(node.getNodeValue());
        break;
    case Node.COMMENT_NODE:
        out.write( /*indent + */ "<!--" + node.getNodeValue() + "-->");
        break;
    default:
        System.out.println("Got node: " + node.getNodeName());
        break;
    }
}

From source file:Main.java

/**
 * Serialise the supplied W3C DOM subtree.
 *
 * @param node The DOM node to be serialized.
 * @param format Format the output./*w w w  . ja  v  a  2s  . c o m*/
 * @param writer The target writer for serialization.
 * @throws DOMException Unable to serialise the DOM.
 */
public static void serialize(final Node node, boolean format, Writer writer) throws DOMException {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        serialize(node.getChildNodes(), format, writer);
    } else {
        serialize(new NodeList() {
            public Node item(int index) {
                return node;
            }

            public int getLength() {
                return 1;
            }
        }, format, writer);
    }
}

From source file:Main.java

public static void setByPath(Document doc, String path, Node in) {
    Node node = getNodeByPath(doc, path);
    if (in.getNodeType() == Node.DOCUMENT_NODE) {
        in = in.getFirstChild();/*w  w w.j  a  va2 s . c  o m*/
    }
    Node newNode = doc.importNode(in, true);
    node.getParentNode().replaceChild(newNode, node);
}

From source file:Main.java

/**
 * Generates an XPath expression that will return only the given node as its
 * result. This method only works for element, text, document and PI nodes.
 *
 * @param node the node to generate an XPath expression for. This node must
 * be an element node, a text node, a document node, or a processing
 * instruction node./*  w  ww .ja  v a  2s  . com*/
 * @return an XPath expression that will return only the given node as its
 * result.
 * @exception IllegalArgumentException if the given node is not an element,
 * text, document or PI node.
 */
public static String getXPathExprFromNode(Node node) throws IllegalArgumentException {
    short nodeType = getNodeType(node);

    switch (nodeType) {
    case Node.ELEMENT_NODE:
    case Node.TEXT_NODE:
    case Node.PROCESSING_INSTRUCTION_NODE:
        return getXPathFromVector(getVectorPathFromNode(node));

    case Node.DOCUMENT_NODE:
        return "/";

    default:
        throw new IllegalArgumentException("Only works for element, text, " + "document, and PI nodes.");
    }
}

From source file:Main.java

/**
 * Serializes the DOM-tree to a stringBuffer. Recursive method!
 *
 * @param node Node to start examining the tree from.
 * @param writeString The StringBuffer you want to fill with xml.
 * @return The StringBuffer containing the xml.
 * //  w  w w. j a  va2  s .  c  o m
 * @since 2002-12-12
 * @author Mattias Bogeblad
 */

public static StringBuffer serializeDom(Node node, StringBuffer writeString) {
    int type = node.getNodeType();
    try {
        switch (type) {
        // print the document element
        case Node.DOCUMENT_NODE: {
            writeString.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            writeString = serializeDom(((Document) node).getDocumentElement(), writeString);
            break;
        }
        // print element with attributes
        case Node.ELEMENT_NODE: {
            writeString.append("<");
            writeString.append(node.getNodeName());
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                String outString = " " + attr.getNodeName() + "=\""
                        + replaceSpecialCharacters(attr.getNodeValue()) + "\"";
                writeString.append(outString);
            }
            writeString.append(">");
            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++)
                    writeString = serializeDom(children.item(i), writeString);
            }
            break;
        }
        // handle entity reference nodes
        case Node.ENTITY_REFERENCE_NODE: {
            String outString = "&" + node.getNodeName() + ";";
            writeString.append(outString);
            break;
        }
        // print cdata sections
        case Node.CDATA_SECTION_NODE: {
            String outString = "<![CDATA[" + node.getNodeValue() + "]]>";
            writeString.append(outString);
            break;
        }
        // print text
        case Node.TEXT_NODE: {
            writeString.append(replaceSpecialCharacters(node.getNodeValue()));
            break;
        }
        // print processing instruction
        case Node.PROCESSING_INSTRUCTION_NODE: {
            String data = node.getNodeValue();
            String outString = "<?" + node.getNodeName() + " " + data + "?>";
            writeString.append(outString);
            break;
        }
        }
        if (type == Node.ELEMENT_NODE) {
            String outString = "</" + node.getNodeName() + ">";
            writeString.append(outString);
        }
    } catch (Exception e) {

    }
    return writeString;
}

From source file:Main.java

public static String getXPath(Node node) {
    if (null == node)
        return null;

    // declarations
    Node parent = null;/* ww w . j a va 2 s . co m*/
    Stack<Node> hierarchy = new Stack<Node>();
    StringBuilder buffer = new StringBuilder();

    // push element on stack
    hierarchy.push(node);

    parent = node.getParentNode();
    while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) {
        // push on stack
        hierarchy.push(parent);

        // get parent of parent
        parent = parent.getParentNode();
    }

    // construct xpath
    Object obj = null;
    while (!hierarchy.isEmpty() && null != (obj = hierarchy.pop())) {
        Node n = (Node) obj;
        boolean handled = false;

        // only consider elements
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element e = (Element) n;

            // is this the root element?
            if (buffer.length() == 0) {
                // root element - simply append element name
                buffer.append(n.getNodeName());
            } else {
                // child element - append slash and element name
                buffer.append("/");
                buffer.append(n.getNodeName());

                if (n.hasAttributes()) {
                    // see if the element has a name or id attribute
                    if (e.hasAttribute("id")) {
                        // id attribute found - use that
                        buffer.append("[@id='" + e.getAttribute("id") + "']");
                        handled = true;
                    } else if (e.hasAttribute("name")) {
                        // name attribute found - use that
                        buffer.append("[@name='" + e.getAttribute("name") + "']");
                        handled = true;
                    }
                }

                if (!handled) {
                    // no known attribute we could use - get sibling index
                    int prev_siblings = 1;
                    Node prev_sibling = n.getPreviousSibling();
                    while (null != prev_sibling) {
                        if (prev_sibling.getNodeType() == n.getNodeType()) {
                            if (prev_sibling.getNodeName().equalsIgnoreCase(n.getNodeName())) {
                                prev_siblings++;
                            }
                        }
                        prev_sibling = prev_sibling.getPreviousSibling();
                    }
                    buffer.append("[" + prev_siblings + "]");
                }
            }
        }
    }

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

From source file:Main.java

protected static void print(PrintStream out, Node node) {
    if (node == null)
        return;//w w w. jav a2  s .  c om
    short type = node.getNodeType();
    switch (type) {
    case Node.DOCUMENT_NODE: {
        out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        // out.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n");
        NodeList nodelist = node.getChildNodes();
        int size = nodelist.getLength();
        for (int i = 0; i < size; i++)
            print(out, nodelist.item(i));
        break;
    }

    case Node.DOCUMENT_TYPE_NODE: {
        DocumentType docType = (DocumentType) node;
        out.print("<!DOCTYPE " + getDocumentTypeData(docType) + ">\n");
        break;
    }

    case Node.ELEMENT_NODE: {
        out.print('<');
        out.print(node.getNodeName());
        NamedNodeMap map = node.getAttributes();
        if (map != null) {
            int size = map.getLength();
            for (int i = 0; i < size; i++) {
                Attr attr = (Attr) map.item(i);
                out.print(' ');
                out.print(attr.getNodeName());
                out.print("=\"");
                out.print(normalize(attr.getNodeValue()));
                out.print('"');
            }
        }

        if (!node.hasChildNodes())
            out.print("/>");
        else {
            out.print('>');
            NodeList nodelist = node.getChildNodes();
            int numChildren = nodelist.getLength();
            for (int i = 0; i < numChildren; i++)
                print(out, nodelist.item(i));

            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: {
        NodeList nodelist = node.getChildNodes();
        if (nodelist != null) {
            int size = nodelist.getLength();
            for (int i = 0; i < size; i++)
                print(out, nodelist.item(i));

        }
        break;
    }

    case Node.CDATA_SECTION_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.TEXT_NODE: {
        out.print(normalize(node.getNodeValue()));
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: {
        out.print("<?");
        out.print(node.getNodeName());
        String s = node.getNodeValue();
        if (s != null && s.length() > 0) {
            out.print(' ');
            out.print(s);
        }
        out.print("?>");
        break;
    }

    case Node.COMMENT_NODE: {
        out.print("<!--");
        out.print(node.getNodeValue());
        out.print("-->");
        break;
    }

    default: {
        out.print(normalize(node.getNodeValue()));
        break;
    }
    }
    out.flush();
}

From source file:Main.java

private static String getTextContent(final 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://from  w  ww.  j ava  2  s  .  c  o  m
        return null;
    }
}

From source file:DOMDump.java

private static void dumpLoop(Node node, String indent) {
    switch (node.getNodeType()) {
    case Node.CDATA_SECTION_NODE:
        System.out.println(indent + "CDATA_SECTION_NODE");
        break;/* ww w.ja va2s .  c om*/
    case Node.COMMENT_NODE:
        System.out.println(indent + "COMMENT_NODE");
        break;
    case Node.DOCUMENT_FRAGMENT_NODE:
        System.out.println(indent + "DOCUMENT_FRAGMENT_NODE");
        break;
    case Node.DOCUMENT_NODE:
        System.out.println(indent + "DOCUMENT_NODE");
        break;
    case Node.DOCUMENT_TYPE_NODE:
        System.out.println(indent + "DOCUMENT_TYPE_NODE");
        break;
    case Node.ELEMENT_NODE:
        System.out.println(indent + "ELEMENT_NODE");
        break;
    case Node.ENTITY_NODE:
        System.out.println(indent + "ENTITY_NODE");
        break;
    case Node.ENTITY_REFERENCE_NODE:
        System.out.println(indent + "ENTITY_REFERENCE_NODE");
        break;
    case Node.NOTATION_NODE:
        System.out.println(indent + "NOTATION_NODE");
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        System.out.println(indent + "PROCESSING_INSTRUCTION_NODE");
        break;
    case Node.TEXT_NODE:
        System.out.print(indent + "TEXT_NODE");
        System.out.println(" : " + node.getTextContent());
        break;
    default:
        System.out.println(indent + "Unknown node");
        break;
    }

    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        dumpLoop(list.item(i), indent + "   ");
    }
}

From source file:Main.java

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