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 getFirstChild(Element parent, String name) {
    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)) {
            return (Element) n;
        }/*from  www . j a  v a 2 s .  c o m*/
    }
    return null;
}

From source file:Main.java

/**
 *///from   w  w  w . ja  v a2 s . co m
public static String getComment(Element searchIn, int commentIndex) {
    NodeList list = searchIn.getChildNodes();
    int counter = 0;
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n.getNodeType() == Node.COMMENT_NODE) {
            if (counter == commentIndex) {
                return n.getTextContent();
            }
            counter++;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Set the value of element/*from w ww  . j a  v  a2s .c  om*/
 *
 * @param element
 * @param val
 */
public static void setElementValue(Element element, String val) {
    Node node = element.getOwnerDocument().createTextNode(val);
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);
        if (nd.getNodeType() == Node.TEXT_NODE) {
            nd.setNodeValue(val);
            return;
        }
    }
    element.appendChild(node);
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    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)) {
            al.add((Element) n);/*from ww w  . j  a va2s.c  o m*/
        }
    }
    return al.toArray(new Element[al.size()]);
}

From source file:Main.java

/**
 * Returns the first node in the node list that is an element.
 * /* w  w w . j a v  a2s  . com*/
 * @param nodes The list of nodes that contains the element to be
 *              determined.
 * 
 * @return The first element in the given node list.
 */
public static Node getFirstElement(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

public static boolean isElementNode(Node n) {
    return (n.getNodeType() == Node.ELEMENT_NODE);
}

From source file:Main.java

public static String getText(Element e) {
    NodeList nl = e.getChildNodes();
    int max = nl.getLength();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            return n.getNodeValue();
        }/*from   w  ww  .  j a  v a2s  .  com*/
    }
    return "";
}

From source file:Main.java

public static Element getLastChild(Element e) {
    if (e == null)
        return null;
    Node n = e.getLastChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getPreviousSibling();// w  ww.  j a v  a 2 s  .  co m
    return (Element) n;
}

From source file:Main.java

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

        NodeList nodeList = node.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeName().equals(nodeName)) {
                return n;
            }//from   www.  ja v  a  2 s  . c o  m
        }
    }

    return null;
}

From source file:Main.java

public static List<Node> getChildNodes(Node dataNode) {
    List<Node> returnList = new ArrayList<Node>();

    NodeList list = dataNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() != Node.TEXT_NODE) {
            returnList.add(node);/*from   w  w w.j  a  v  a2s  . c  om*/
        }
    }

    return returnList;
}