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

/**
 * Removes all Whitespace Noted from the specified element.
 * @param e Element// www.j  a  v a2  s.  c  o m
 */
private static void removeWhitespaceNodes(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
            e.removeChild(child);
        } else if (child instanceof Element) {
            removeWhitespaceNodes((Element) child);
        }
    }
}

From source file:Main.java

public static Element getElementByTagName(Element element, String tagName) {
    NodeList nodeList = element.getElementsByTagName(tagName);
    if (nodeList.getLength() == 0) {
        return null;
    }//from   w w  w .j  a  v  a  2 s  . co m
    Node node = nodeList.item(0);
    return (Element) node;
}

From source file:Main.java

/**
 * @param node/*from  ww  w.  j  ava2  s . c  om*/
 * @param name
 * @return all children nodes whose names contain <code>like</code>
 */
public static List<Node> findChildrenLike(Node node, String like) {
    List<Node> ret = new LinkedList<Node>();
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (child.getNodeName().indexOf(like) > 0) {
            ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

/**Get a NodeLIst of child nodes, ignoring nodes that have White space
 * @param e/*from w  ww.j av  a2  s  .  co m*/
 * @return
 */
public static NodeList getChildNodesNoWS(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0) {
            e.removeChild(child);
        }

    }
    return e.getChildNodes();
}

From source file:Main.java

public static Stream<Node> stream(NodeList nodeList) {
    Node[] nodes = new Node[nodeList.getLength()];

    for (int i = 0; i < nodeList.getLength(); i++) {
        nodes[i] = nodeList.item(i);/* w ww  . j av  a  2 s.  c o m*/
    }
    return Stream.of(nodes);
}

From source file:Main.java

public static Element getChildElement(Element elem, String name) {
    NodeList nodes = elem.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(name)) {
            return (Element) node;
        }/*from   w  w w  . j a  v a  2s  .  co m*/
    }
    return null;
}

From source file:Main.java

/**
 * @param node/* ww w  .  j a va2s .  c om*/
 * @param name
 * @return all children nodes with the given name
 */
public static List<Node> findChildren(Node node, String name) {
    List<Node> ret = new LinkedList<Node>();
    NodeList nl = node.getChildNodes();
    int len = nl.getLength();
    Node child;
    for (int i = 0; i < len; i++) {
        child = nl.item(i);
        if (name.equals(child.getNodeName())) {
            ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

public static Element getFirstElement(Element element) {
    Element result = null;/*from   w ww  .  j av  a2 s.  c om*/
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof Element) {
            result = (Element) nl.item(i);
        }
    }
    return result;
}

From source file:Main.java

public static final Boolean parameterExists(final Element e, final String name) {
    NodeList nl = e.getElementsByTagName(name);
    if (nl.getLength() > 0) {
        return true;
    } else {/*from  w  w w  .ja v a 2s .  c  o  m*/
        return false;
    }
}

From source file:Main.java

public static String getNodeContent(Document document, String nodeName) {
    NodeList nl = document.getElementsByTagName(nodeName);
    if (0 == nl.getLength()) {
        return "";
    } else {/*from  w w w.  jav  a  2 s .co  m*/
        return nl.item(0).getTextContent();
    }
}