Example usage for org.w3c.dom Node getNodeType

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

Introduction

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

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static Element getOnlyElementChild(Element e, String tag, RuntimeException exc) {
    NodeList nl = e.getChildNodes();
    Element result = null;/*  w ww  .j av a  2s.  c om*/
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (result != null) // more than one Element children 
                throw exc;
            result = (Element) n;
        }
    }

    if (result == null // zero Element children
            || !result.getTagName().equals(tag)) // wrong tag
        throw exc;
    return result;
}

From source file:Main.java

public static Element getLastChild(Element parent, String name) {
    Element child = null;/*from  ww w  .  j a v  a 2  s  . co  m*/
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            child = (Element) n;
        }
    }
    return child;
}

From source file:Main.java

public static String getText(Node node) {
    String text = "";
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            text += child.getNodeValue() + " ";
        }//from   w ww  .  j a va2  s  .co  m
    }
    return text;
}

From source file:Main.java

public static Element getNextSiblingElementNode(Node node) {
    Node nextNode = node.getNextSibling();
    while (nextNode.getNodeType() != Node.ELEMENT_NODE) {
        nextNode = nextNode.getNextSibling();
    }/*  w w  w .  j  a v a  2  s.  c o  m*/

    return (Element) nextNode;
}

From source file:Main.java

public static Node getNode(Node node, String nodeName) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(nodeName);
        if (nodeList.getLength() > 0) {
            return nodeList.item(0);
        }//from w  w w .j  a  v a  2  s .co m

    }

    return null;
}

From source file:Main.java

private final static boolean isParentNode(Node element) {
    NodeList elements = element.getChildNodes();
    for (int i = 0; i < elements.getLength(); i++) {
        Node child = elements.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }//from   ww w  .j  a va 2  s.  c o  m
    }

    return false;
}

From source file:Main.java

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

From source file:Main.java

private static boolean checkNodeTypes(Node childNode) {
    short nodeType = childNode.getNodeType();
    if (nodeType == Node.ELEMENT_NODE) {
        cleanEmptyTextNodes(childNode); // recurse into subtree
    }/*ww  w  .  ja v  a 2 s .  c o  m*/
    if (nodeType == Node.ELEMENT_NODE || nodeType == Node.CDATA_SECTION_NODE || nodeType == Node.COMMENT_NODE) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

public static String getNodeText(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        NodeList childNodes = node.getChildNodes();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < childNodes.getLength(); i++) {
            if (childNodes.item(i).getNodeType() == Node.TEXT_NODE) {
                Node myNode = childNodes.item(i);
                buffer.append(myNode.getNodeValue());
            }//from w w w.j a v  a 2 s .  c o m
        }
        return buffer.toString();
    }
    return "";
}

From source file:Main.java

public static List<Element> getChildElements(Element elem, String name) {
    NodeList nodes = elem.getElementsByTagName(name);
    List<Element> elements = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            elements.add((Element) node);
        }/* w  ww  .j a v  a 2s. c om*/
    }
    return elements;
}