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

/**
 * //from  www  .ja  va  2 s .  co m
 * @param e
 * @return
 */
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().trim();
        }
    }
    return "";
}

From source file:Main.java

/**
 * Find all child node(immediate children only) under current node that is an element node and node name is tagName
 * This is  a combination of getChildNodes() and getElementByTagName().
 * Also this helper method returns an iterable node list for convinient use in the foreach statement.
 * Only element node directly under currentNode are returned.(i.e no grandchildren).   
 * The order of the children are maintained (removing non-element node) 
 * @param currentNode/*ww  w . ja v a2s  . co m*/
 * @param tagName - case sensitive
 * @return list of element nodes equals tagname (case sensitive) 
 */
public static List<Node> getChildElementsByTagName(Node currentNode, String tagName) {
    List<Node> results = new ArrayList<Node>();
    NodeList childNodes = currentNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if ((child.getNodeType() == Node.ELEMENT_NODE) && child.getNodeName().equals(tagName))
            results.add(child);
    }
    return results;
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName) {
    NodeList nodeList = parentElement.getElementsByTagName(nodeName);
    if (nodeList.getLength() == 1) {
        Node node = nodeList.item(0);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) node;
        }// w  ww  .  jav a 2s  .  c om
    }
    return null;
}

From source file:Main.java

/**
 * Gets the first child element./* ww w .j  av  a2 s.co  m*/
 * 
 * @param e the element.
 * @return the first child element.
 */
public static Element getFirstChildElement(Element e) {
    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Attr> getAttributes(Element element) {

    List<Attr> attributes = new LinkedList<Attr>();

    NamedNodeMap namedNodeMap = element.getAttributes();

    for (int i = 0; i < namedNodeMap.getLength(); i++) {
        Node node = namedNodeMap.item(i);
        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
            Attr a = (Attr) node;
            attributes.add(a);/*from  ww w .  j a  v  a 2 s.  com*/
        }
    }

    return attributes;
}

From source file:Main.java

public static Iterator getElementsByTagNames(Element element, String[] tags) {
    List<Element> children = new ArrayList<Element>();
    if (element != null && tags != null) {
        List tagList = Arrays.asList(tags);
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node child = nodes.item(i);
            if (child.getNodeType() == Node.ELEMENT_NODE && tagList.contains(((Element) child).getTagName())) {
                children.add((Element) child);
            }/* w  ww  .  j a  v  a  2s.  c o m*/
        }
    }
    return children.iterator();
}

From source file:Main.java

private static void printNode(NodeList nodeList, int level) {
    level++;/*from  w  w w  .  j  a va  2  s . c  o  m*/
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                System.out.println(node.getNodeName() + "[" + level + "]");
                printNode(node.getChildNodes(), level);
                // how depth is it?
                if (level > depthOfXML)
                    depthOfXML = level;
            }
        }
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public final static Vector subElementList(Element superEle, String subName) {
    Vector v = new Vector();
    NodeList list = superEle.getChildNodes();
    if (list == null) {
        return null;
    }//  ww  w  . j  av  a 2 s .  c o m

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(subName)) {
                v.add(node);
            }
        }
    }
    return v;
}

From source file:Main.java

public static String getElementText(Element element) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.TEXT_NODE) {
            return ((Text) child).getNodeValue();
        }/*  www  .j  a v a 2s.c o m*/
    }
    return "";
}

From source file:Main.java

public static List<Element> getChildElements(String tagName, Element element) {
    LinkedList<Element> result = new LinkedList<>();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(tagName)) {
            result.add((Element) n);
        }//w ww  . j a  v a 2 s. c  o m
    }
    return result;
}