Example usage for org.w3c.dom NodeList getLength

List of usage examples for org.w3c.dom NodeList getLength

Introduction

In this page you can find the example usage for org.w3c.dom NodeList getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in the list.

Usage

From source file:Main.java

public static List<Node> getChildrenByTagName(Node node, String element) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node childNode = childNodes.item(j);
        if ("*".equals(element) || element == null || childNode.getNodeName().equals(element)) {
            result.add(childNode);//  w w  w .  j av  a 2  s  . c o m
        }
    }
    return result;
}

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  w w.  j a v  a  2 s . c o m
        }
    }
    return children.iterator();
}

From source file:Main.java

public static Element getChild(Element element, String tagName) {
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (child.getTagName().equals(tagName))
                return child;
        }/*from   w ww .ja  va  2  s.  c o  m*/
    }
    return null;
}

From source file:Main.java

public static final void setNodeValue(Node node, String value) {
    if (value == null)
        value = "";
    NodeList childNodes = node.getChildNodes();
    int len = childNodes == null ? 0 : childNodes.getLength();
    for (int i = 0; i < len; i++) {
        Node n = childNodes.item(i);
        n.setNodeValue(value);/*  w  w  w  . j a va2s.co m*/
    }
}

From source file:Main.java

public static List<Element> getChildNodes(Element parent, String name) {
    List<Element> result = new ArrayList<Element>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n = childNodes.item(i);
        if (name.equals(n.getNodeName())) {
            result.add((Element) n);
        }//from w ww. j  av a  2  s. c  o  m
    }
    return result;
}

From source file:Main.java

public static Element getFirstChildElementByName(Element root, String tagName) {
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);/* w w w.  j a v a 2 s . c o  m*/
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (tagName.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/***********************************************************************/
private static Node traverseToInnerTag(String tag, Node node) {
    String name = node.getNodeName();
    if (name.equals(tag)) {
        return node;
    } else {//from   ww  w  .  j a v a 2s.  c  om
        NodeList childNodes = node.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = traverseToInnerTag(tag, childNodes.item(i));
            if (child != null) {
                return child;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Search a first node by name in the document
 * //from  w  w w  . j  a v  a  2s. com
 * @param doc      source document
 * @param nodeName   the node name for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodesByName(Document doc, String nodeName) throws Exception {

    Node node = null;

    NodeList nl = doc.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            break;
        }
    }

    return node;

}

From source file:Main.java

public static List<Node> getChildren(Node node) {
    ArrayList<Node> children = new ArrayList<Node>();
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        children.add(nodeList.item(i));//from  w w w . j a v  a 2s . c  om
    }
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            children.add(attributes.item(i));
        }
    }
    return children;
}

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();
        }//from   w  w  w.j av  a2s  . com
    }
    return "";
}