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 a textual representation of the given node to the specified PrintStream.
 *
 * @param  n    Node that is to be printed.
 * @param  out  The PrintStream to which the node is to be printed.
 * @pre    n != null && out != null
 *///from   ww  w.  j a va2  s  .com
public static void printXMLNode(Node n, PrintStream out) {
    switch (n.getNodeType()) {
    case Node.DOCUMENT_NODE:
        out.println("DOC_ROOT");
        break;

    case Node.ELEMENT_NODE:
        out.println("<" + ((Element) n).getTagName() + ">");
        break;

    case Node.ATTRIBUTE_NODE:
        out.println("@" + ((Attr) n).getName());
        break;

    case Node.TEXT_NODE:
        out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
        break;

    case Node.COMMENT_NODE:
        out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
        break;

    default:
        out.println("Unknown node type: " + n.getNodeType());
    }
}

From source file:Main.java

public static String path(Node n) {
    if (n == null)
        return "";
    switch (n.getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return path(n.getParentNode()) + "/@" + n.getNodeName();
    }//w  w  w. j a  va2 s .co m
    case Node.TEXT_NODE: {
        return path(n.getParentNode()) + "/text()";
    }
    case Node.CDATA_SECTION_NODE: {
        return path(n.getParentNode()) + "/cdata()";
    }
    case Node.ELEMENT_NODE: {
        return path(n.getParentNode()) + "/" + n.getNodeName();
    }
    case Node.DOCUMENT_NODE: {
        return "";
    }
    default: {
        return "";
    }
    }
}

From source file:Main.java

public static Element getChildElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }// w w  w  .  jav a2  s .  c  o m
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null
                    && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                    && name.equalsIgnoreCase(item.getNodeName())) {
                return (Element) item;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element findElementNodeByName(Node node, String name) {
    if (node == null) {
        return null;
    }/*from   ww  w.j  a  v a2s  . c  om*/
    NodeList nodeList = node.getChildNodes();
    if (nodeList == null || nodeList.getLength() == 0) {
        return null;
    }

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node item = nodeList.item(i);
        if (item != null
                && (node.getNodeType() == Node.ELEMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE)
                && name.equalsIgnoreCase(item.getNodeName())) {
            return (Element) item;
        } else {
            Element element = findElementNodeByName(item, name);
            if (element != null) {
                return element;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the parent element./*from w ww . ja  v a  2  s .c  o m*/
 * 
 * @param element
 *            the element
 * @return the parent element
 */
public static Element getParentElement(Element element) {
    if (element == null) {
        return null;
    }
    Element parentElement = null;
    Node parentNode = element.getParentNode();
    while (parentNode != null && parentElement == null) {
        if (parentNode.getNodeType() == Node.ELEMENT_NODE) {
            parentElement = (Element) parentNode;
        }
        if (parentNode.getNodeType() == Node.DOCUMENT_NODE) {
            parentElement = ((Document) parentNode).getDocumentElement();
            if (element.isSameNode(parentElement)) {
                parentElement = null;
            }
        }
        parentNode = parentNode.getParentNode();
    }

    return parentElement;
}

From source file:Main.java

/**
 * //w  ww  .j  a v a2s .c  o m
 * @param currentNode
 * @param tagName
 * @param attributeValue
 * @return
 */
public static String getTextContentByElementNameANDAttributeValue(Node currentNode, String tagName,
        String attributeValue) {
    String result = "";

    NodeList childNodeList = currentNode.getChildNodes();
    for (int i = 0; i < childNodeList.getLength(); i++) {
        Node childNode = childNodeList.item(i);

        switch (childNode.getNodeType()) {
        case Node.DOCUMENT_NODE:
            break;
        case Node.ELEMENT_NODE:
            Element childElement = (Element) childNodeList.item(i);
            // logger.debug("childElement name : " + childElement.getTagName());
            if (childElement != null && childElement.getNodeName().equals(tagName)) {
                NamedNodeMap attributes = childElement.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node current = attributes.item(j);

                    if (current.getNodeName().equals("type") && current.getNodeValue().equals(attributeValue)) {
                        result = childElement.getTextContent();
                        break;
                    }
                }
            }
        case Node.TEXT_NODE:
            // logger.debug("textElement name : " + currentNode.getNodeValue());
            break;
        case Node.COMMENT_NODE:
            break;
        case Node.PROCESSING_INSTRUCTION_NODE:
            break;
        case Node.ENTITY_REFERENCE_NODE:
            break;
        case Node.DOCUMENT_TYPE_NODE:
            break;
        }
    }

    return result;
}

From source file:Main.java

protected static Node getNodeIfDocument(Node node) {
    if (node.getNodeType() == Node.DOCUMENT_NODE) {
        return ((Document) node).getDocumentElement();
    } else {//  www . j  av  a 2s .  c o m
        return node;
    }
}

From source file:Main.java

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

    for (Node node = xmlNode; node != null; node = node.getParentNode()) {
        if (node.getNodeType() == Node.DOCUMENT_NODE)
            result.insert(0, node.getNodeName());
        else/*from   w w w .j  a va2 s  .  co m*/
            result.insert(0, " -> " + node.getNodeName());
    }

    return result.toString();
}

From source file:Main.java

public static void setXsdSchema(Node node, String schemaURL) {
    Document doc;/*from  ww w  .  ja 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);
    }
}

From source file:Main.java

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