Example usage for org.w3c.dom Element getChildNodes

List of usage examples for org.w3c.dom Element getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Element getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

/**
 * Helper class to help parse the server response.
 * /*  w  ww .  j a v  a  2  s  .  c  o m*/
 * @param nodeList
 * @return
 */
public static String getNodeTrimValue(NodeList nodeList) {
    Element element = (Element) nodeList.item(0);
    String nodeValue = "";

    if (element != null) {
        NodeList itemNodeList = element.getChildNodes();

        int length = itemNodeList.getLength();

        for (int i = 0; i < length; i++) {
            Node node = ((Node) itemNodeList.item(i));
            if (node != null)
                nodeValue += node.getNodeValue();
        }

        if (nodeValue != null && !nodeValue.equals("")) {
            return nodeValue.trim();
        } else {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns a list of children (class Element) with the given tag name
 *//*from w  w  w .  ja  va  2  s . c om*/
public static List<Element> getChilden(final Element element, final String tagName) {
    final List<Element> children = new ArrayList<Element>();
    final NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        final Node item = nodes.item(i);
        if (!(item instanceof Element)) {
            continue;
        }
        final Element elem = (Element) item;
        if (tagName.equals(elem.getTagName())) {
            children.add(elem);
        }
    }
    return children;
}

From source file:com.dinstone.jrpc.spring.EndpointBeanDefinitionParser.java

public static Element getChildElement(Element ele, String childName) {
    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);/*from   w  w w. jav  a 2  s  .  c o m*/
        if (node instanceof Element && nodeMatch(node, childName)) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Utils.java

public static Element[] getElements(Document document, Element parent) {
    if (parent == null) {
        return new Element[] {};
    }//from  ww w .j  av a 2s  . c om

    NodeList nl = parent.getChildNodes();
    ArrayList al = new ArrayList();

    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);

        if (n instanceof Element) {
            al.add((Element) nl.item(i));
        }
    }

    Element[] ret = new Element[al.size()];
    Iterator it = al.iterator();
    int i = 0;
    while (it.hasNext()) {
        ret[i] = (Element) it.next();
        i++;
    }

    return ret;
}

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 . ja v a  2 s.c o m*/
    }
    return true;
}

From source file:Main.java

public static ArrayList getChildTextNodeValues(Node theNode) {
    ArrayList arrayList = new ArrayList();
    if (theNode.getNodeType() != Node.TEXT_NODE) {
        Element theNodeElement = (Element) theNode;
        NodeList theNodeList = theNodeElement.getChildNodes();
        for (int i = 0; i < theNodeList.getLength(); i++) {
            Node node = getTextNode(theNodeList.item(0));
            if (node != null) {
                String s = node.getNodeValue();
                arrayList.add(s);//from  w  w w. j  ava 2s.co  m
            }
        }
    }
    return arrayList;
}

From source file:Main.java

public static String getElementTextNodeValue(Node root, String nodeString) {
    Element elem = getElementNode(root, nodeString);
    if (elem == null) {
        return null;
    }// w w  w  .  j ava 2 s  .  c o  m
    NodeList nlist = elem.getChildNodes();

    if (nlist == null) {
        return null;
    }
    for (int i = 0; i < nlist.getLength(); i++) {
        if (nlist.item(i).getNodeType() == Node.TEXT_NODE) {
            return nlist.item(i).getNodeValue();
        }
    }

    return null;
}

From source file:Main.java

/**
 * Return an array of direct child elements that have the given element tag
 * name.//from  ww  w.  j a  va  2  s  . c  o m
 * 
 * @param name
 *            Element tag name to search for
 * @param parent
 *            Parent to search
 * @return Array of child elements with the given name
 */
public static Element[] getChildElementsByTagName(String name, Element parent) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        NodeList children = parent.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                Node node = children.item(i);
                if (node instanceof Element) {
                    Element e = (Element) node;
                    if (e.getTagName().equals(name)) {
                        elements.add(e);
                    }
                }
            }
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

/**
 * Takes a number of tags of name 'name' that are children of 'root', and
 * looks for attributes of 'attrib' on them.  Converts attributes to an
 * int and returns in an array./*from w w  w. j  av  a 2 s  . c o  m*/
 */
public static int[] getElementArrayInt(Element root, String name, String attrib) {
    if (root == null)
        return null;

    NodeList nl = root.getChildNodes();
    LinkedList<Integer> elementCache = new LinkedList<Integer>();
    int size = nl.getLength();

    for (int i = 0; i < size; i++) {
        Node node = nl.item(i);
        if (!(node instanceof Element))
            continue;
        Element ele = (Element) node;
        if (!ele.getTagName().equals(name))
            continue;

        String valS = ele.getAttribute(attrib);
        int eleVal = 0;
        try {
            eleVal = Integer.parseInt(valS);
        } catch (Exception e) {
        }

        elementCache.addLast(new Integer(eleVal));
    }

    int[] retArr = new int[elementCache.size()];
    Iterator<Integer> it = elementCache.iterator();
    int idx = 0;
    while (it.hasNext()) {
        retArr[idx++] = it.next().intValue();
    }

    return retArr;
}

From source file:Main.java

public static Element getChild(Element element, String fieldName) {
    if ((element == null) || (fieldName == null) || fieldName.equals("")) {
        return null;
    }//from  w  ww .  ja  v a 2  s .co m
    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;
}