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

public static Element[] getChildrenByAttrubte(Element parent, String attrName, String attrValue) {
    NodeList nodes = parent.getChildNodes();
    int len = nodes.getLength();
    Element temp;/*from www  .j a  v  a2  s . co m*/
    List<Element> list = new ArrayList<Element>(len);
    if (nodes != null && len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                temp = (Element) nodes.item(i);
                if (getAttribute(temp, attrName).equalsIgnoreCase(attrValue))
                    list.add(temp);
            }
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

public static String getBody(Element element) {
    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() == 0)
        return null;

    Node firstNode = nodes.item(0);
    if (firstNode == null)
        return null;

    String nodeValue = firstNode.getNodeValue();
    return replace(nodeValue);
}

From source file:Main.java

/**
 * @param child//from  ww w.  java2 s  .  c  o m
 * @return the single child element or null
 * @throws Exception if the child is present multiple times
 */
public static Element getChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equalsIgnoreCase(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    if (ret == null) {
        return null;
    } else {
        return ret;
    }
}

From source file:Main.java

/**
 * Returns the child element with the specified name for the specified element.
 *
 * @param element the parent element//from w ww .  j  a v  a2  s. c  o m
 * @param name    the name of the child element to return
 *
 * @return the child element or <code>null</code> if a child element with the specified name
 *         could not be found
 */
public static Element getChildElement(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                return childElement;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Returns <code>true</code> if the specified element has a child with the specified name or
 * <code>false</code> otherwise.
 *
 * @param element the parent element/*from w  w  w.j  ava 2  s.  co  m*/
 * @param name    the name of the child element
 *
 * @return <code>true</code> if the specified element has a child with the specified name or
 *         <code>false</code> otherwise
 */
public static boolean hasChildElement(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                return true;
            }
        }
    }

    return false;
}

From source file:Main.java

/**
 * Returns the text content for the child element with the specified name for the specified
 * element./* www.  j av  a2  s  .  c  o  m*/
 *
 * @param element the parent element
 * @param name    the name of the child element to return
 *
 * @return the text content for the child element or <code>null</code> if a child element with
 *         the specified name could not be found
 */
public static String getChildElementText(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                return childElement.getTextContent();
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * @param name The name of the child elements you want
 * @return a List of child Elements//from w w w  .jav  a 2s.c  o m
 */
public static List<Element> getChildren(Element element, String name) throws Exception {
    NodeList nodes = element.getChildNodes();
    ArrayList<Element> ret = new ArrayList<Element>(nodes.getLength());
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equals(name) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            ret.add((Element) childNode);
        }
    }
    return ret;
}

From source file:Main.java

public static String getText(Element paramElement) {
    NodeList localNodeList = paramElement.getChildNodes();
    int i = localNodeList.getLength();
    for (int j = 0; j < i; ++j) {
        Node localNode = localNodeList.item(j);
        if (localNode.getNodeType() == 3)
            return localNode.getNodeValue();
    }//from  w  ww . j  av  a2  s .c  om
    return "";
}

From source file:Main.java

public static Node getFirstChildByType(Element element, short nodeType) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i).getNodeType() == nodeType) {
            return childNodes.item(i);
        }//from   w  w  w .ja v a  2 s  . c o  m
    }
    return null;
}

From source file:Utils.java

/**
 * /*from w w w  .  j  av a 2 s  .c o m*/
 */
public static String getElementText(Element element) {
    StringBuffer buffer = new StringBuffer();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
            buffer.append(node.getNodeValue());
        }
    }
    return buffer.toString();
}