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 String getElementText(Element n) {
    NodeList childNodes = n.getChildNodes();
    String result = new String();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            result += node.getNodeValue();
        }/*from  w  w w .  j a v  a2 s. c  om*/
    }
    return result;
}

From source file:Main.java

/**
 * We get a object that connected text node of the child node and the CDATA section as character string.
 * @param node/*from   w  ww.ja va  2  s .  com*/
 * @return
 */
public static String getChildText(Node node) {
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < node.getChildNodes().getLength(); i++) {
        Node n = node.getChildNodes().item(i);
        if (n.getNodeType() == Node.TEXT_NODE || n.getNodeType() == Node.CDATA_SECTION_NODE) {
            buf.append(n.getNodeValue());
        }
    }
    return buf.toString();
}

From source file:Main.java

static public String getDocumentComment(Document doc) {
    String docComment = null;//w  ww .j  ava  2s . co m
    try {
        NodeList childs = doc.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node child = childs.item(i);
            if (child.getNodeType() == Node.COMMENT_NODE) {
                docComment = child.getNodeValue();
            }
        }
        return docComment;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * Get the first node with a given name and returns it
 *
 * @param root the node that contains the children to search within
 * @param name the name of the node/* w w  w.  j  a v  a 2 s  . c om*/
 * @return a node with given or null if not found
 */
public static Node find(Node root, String name) {
    final NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        final Node subnode = list.item(i);
        if (subnode.getNodeType() == Node.ELEMENT_NODE) {
            if (subnode.getNodeName().equals(name)) {
                return subnode;
            }
        }
    }
    return null;
}

From source file:Main.java

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

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType())
            return (child);
    }//from w  w w.  j  a v a  2  s.  c  om

    return (null);
}

From source file:Main.java

public static Node[] getChildNodes(Node node, short type) {
    NodeList children = node.getChildNodes();
    if (children == null) {
        return new Node[0];
    }/*from w w w .  j  av  a2 s. c o m*/
    int n = children.getLength();
    List<Node> elnodelist = new ArrayList<Node>(n);
    for (int i = 0; i < n; ++i) {
        Node childnode = children.item(i);
        if (childnode.getNodeType() == type) {
            elnodelist.add(childnode);
        }
    }
    Node[] empty = {};
    return elnodelist.toArray(empty);
}

From source file:Main.java

public static String getTextTag(Node node, String tagName) {
    Element elem = (Element) node;
    NodeList list = elem.getElementsByTagName(tagName);
    if (list.getLength() == 0)
        return null;
    Node child = list.item(0).getFirstChild();
    if (child.getNodeType() == Node.TEXT_NODE)
        return child.getNodeValue();
    return null;/*  w  w  w  .ja  v  a  2  s.c  o m*/
}

From source file:Main.java

static public String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }//  w w  w  .  ja  v  a  2s  . com
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * Get the trimmed text of a text (simple content) element.
 * /*from w w w.j a  v  a  2  s  .c  o  m*/
 * @param e The element.
 * @return The text of the specified element, null if it has no text nodes.
 */
public static String getText(Element e) {
    if (e == null)
        return null;
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            String s = t.getData();
            return s == null ? null : s.trim();
        }
    }
    return null;
}

From source file:Main.java

public static ArrayList<Node> getChildNodesWithTagName(String tagName, Element parentElement) {
    NodeList childNodes = parentElement.getChildNodes();

    ArrayList<Node> childNodesWithTagName = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals(tagName)) {
            childNodesWithTagName.add(childNode);
        }//from   ww w  .  j  av  a2s.c o m
    }

    return childNodesWithTagName;
}