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

/**
 * Get first Text node of the Element as String value
 * @param elem// ww w.ja v  a  2  s  .c o m
 * @return
 */
public static String getFirstTextValue(Element elem) {
    if (elem != null) {
        Node fc = elem.getFirstChild();
        if (null != fc && fc.getNodeType() == Node.TEXT_NODE) {
            return ((Text) fc).getData();
        }
    }
    return null;
}

From source file:Main.java

private static void traverseNodes(Element parent, String tagName, List<Element> lst) {
    if (parent.getTagName().indexOf(tagName) != -1) {
        lst.add(parent);/*from   w  w w.  ja va  2s . c  om*/
        return;
    } else {
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item.getNodeType() == Node.ELEMENT_NODE) {
                traverseNodes((Element) item, tagName, lst);
            }
        }
    }
}

From source file:Main.java

public static String[] getNodesValue(Element node, String nodeName) {
    List<String> ret = new ArrayList<String>();

    NodeList ndLs = node.getElementsByTagName(nodeName);
    for (int s = 0; s < ndLs.getLength(); s++) {
        Node fstNode = ndLs.item(s);
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add(fstNode.getTextContent());
        }/*www.j  a  va 2 s.com*/
    }

    return ret.toArray(new String[0]);
}

From source file:Main.java

public static Element getFirstChild(Element e) {
    if (e == null)
        return null;
    Node n = e.getFirstChild();
    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
        n = n.getNextSibling();//from www.  j  a  va  2s. c o m
    return (Element) n;
}

From source file:Main.java

public static boolean isElement(Node node) {
    return node.getNodeType() == Node.ELEMENT_NODE;
}

From source file:Main.java

public static String getNodeValue(Node node) {
    if (node == null) {
        return null;
    }//from   w ww  .j  a va2 s .co  m

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            return child.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElementsByName(Element root, String tagName) {
    List<Element> tags = new LinkedList<Element>();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from  w  ww. j a  v a2s.  c  om*/
        if (tagName.equals(child.getNodeName())) {
            tags.add((Element) child);
        }
    }
    return tags;
}

From source file:Main.java

/**
 * Gets an element from the XML/*from  w  ww  .  j av  a2 s  . c o  m*/
 * 
 * @param elementTag
 * @param fromElement
 * @return
 */
public static Element getElement(String elementTag, Element fromElement) {
    Element element = null;
    NodeList nodeList = fromElement.getElementsByTagName(elementTag);
    if (nodeList.getLength() > 0) {
        Node node = nodeList.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            element = (Element) node;
        }
    }
    return element;
}

From source file:Main.java

/**
 * Gets the previous comment.//  ww  w  . j  av  a  2s  .com
 * 
 * @param element
 *            the element
 * @return the previous comment
 */
public static String getPreviousComment(Node element) {
    while (element.getPreviousSibling() != null) {
        Node prev = element.getPreviousSibling();
        if (prev.getNodeType() == Node.COMMENT_NODE) {
            return prev.getTextContent();
        } else if (prev.getNodeType() == Node.TEXT_NODE) {
            return getPreviousComment(prev);
        } else if (prev.getNodeType() == Node.ELEMENT_NODE) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/** Look for Element node.
 *
 *  <p>Checks the node and its siblings.
 *  Does not descent down the 'child' links.
 *
 *  @param node Node where to start.//from   w ww  .  j a v  a2  s .c o  m
 *  @return Returns node, next Element sibling or <code>null</code>.
 */
public static final Element findElement(Node node) {
    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
        node = node.getNextSibling();
    }
    return null;
}