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

/**
 * Gets <code>CDATASection</code> element for node.
 *
 * @param node node with text./*from   w w  w.  ja va 2s . co m*/
 * @return text, that contains the specified node.
 */
public static String getNodeCDATASection(final Node node) {
    //        node.normalize();
    final NodeList list = node.getChildNodes();
    for (int i = 0, len = list.getLength(); i < len; i++) {
        final Node child = list.item(i);
        if (child.getNodeType() == Node.CDATA_SECTION_NODE)
            return child.getNodeValue();
    }
    return null;
}

From source file:Main.java

/**
 * Returns a list of the direct child {@link Element}s of the given parent.
 * /*w w  w.j  a  va2s . c om*/
 * @param parent The parent element.
 */
public static List<Element> getChildElements(Element parent) {
    List<Element> childElements = new LinkedList<Element>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode instanceof Element)
            childElements.add((Element) childNode);
    }
    return childElements;
}

From source file:Main.java

public static List<Node> getChildNodes(Node node, String childTag) {
    ArrayList<Node> nodes = new ArrayList<Node>();
    if (node == null)
        return nodes;
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode != null && childNode.getLocalName() != null
                && childNode.getLocalName().equals(childTag)) {
            nodes.add(childNode);/*from  w w  w.j  a v a  2s . co  m*/
        }
    }
    return nodes;
}

From source file:Main.java

public static List<? extends Node> get_childs(Node parent, String child_name) {
    List<Node> result = new LinkedList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            result.add(child);//from w  w w. ja v a  2 s  . c om
    }
    return result;
}

From source file:Main.java

private static void registerIDs(Document doc, Node node, Map<String, Node> idMap) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        if (node.getAttributes().getNamedItem("id") != null) {
            final String id = node.getAttributes().getNamedItem("id").getNodeValue();
            idMap.put(id, (Element) node);
        }/*from  w  w  w  .  ja va 2s. c  om*/
    }
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        registerIDs(doc, children.item(i), idMap);
    }

}

From source file:Main.java

private static boolean elementIsRedundant(Element element) {
    if (element.hasAttributes())
        return false;
    if (!element.hasChildNodes())
        return true;
    NodeList children = element.getChildNodes();
    int childrenCount = children.getLength();
    for (int i = 0; i < childrenCount; ++i) {
        Node child = children.item(i);
        String value = child.getNodeValue();
        if (value != null && !value.matches("\\s*")) {
            return false; // Found non-whitespace text
        }/*  w w w  .j  ava 2 s  .co  m*/
    }
    return true;
}

From source file:Main.java

/**
 * Get the first child element of the specified element, null if it has no child elements.
 * /*  ww  w .j  a  va  2  s .co  m*/
 * @param root The element to search.
 * @return The first child element, null if it has none.
 */
public static Element getFirstChild(Element root) {
    if (root == null)
        return null;
    NodeList lst = root.getChildNodes();
    final int n = lst.getLength();
    for (int i = 0; i < n; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
    }
    return null;
}

From source file:Main.java

public static Element getChild(Element element, String fieldName) {
    if ((element == null) || (fieldName == null) || fieldName.equals("")) {
        return null;
    }//from w w  w  .  ja  v  a 2  s.c om
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if ((child instanceof Element) && child.getNodeName().equals(fieldName)) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

public static Element fetchSubElement(Element e, String subElementName) {
    if (e == null || subElementName == null) {
        return null;
    }//from  w w w  .  j  ava  2  s . c  o m

    subElementName = subElementName.toUpperCase();

    NodeList list = e.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i) instanceof Element) {
            Element element = (Element) list.item(i);
            if (element.getTagName().toUpperCase().equals(subElementName)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

public static Element getChildByAttrName(Node node, String attrName, String attrValue) {

    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE) {
            Element el = (Element) n;
            if (attrValue.equals(el.getAttribute(attrName))) {
                return el;
            }//from  w  w w . j  a  va 2 s. co m
        }
    }

    return null;
}