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

/**
 * get all child elements matching the given tag 
 * //from   w  w  w .  j a  v  a 2  s.  c o m
 * @param tag
 * @param searchIn
 * @return
 */
public static ArrayList<Element> getChildElementsByTag(String tag, Element searchIn) {
    ArrayList<Element> toReturn = new ArrayList<Element>();
    NodeList list = searchIn.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n instanceof Element && ((Element) n).getTagName().equals(tag)) {
            toReturn.add((Element) n);
        }

    }
    return toReturn;
}

From source file:Main.java

private static void resolveLeafNodeValue(Node node) {
    if (node != null) {
        Element element = (Element) node;
        NodeList childNodeList = element.getChildNodes();
        for (int j = 0; j < childNodeList.getLength(); j++) {
            Node chileNode = childNodeList.item(j);
            if (!chileNode.hasChildNodes()) {
                String nodeValue = resolveSystemProperty(chileNode.getTextContent());
                childNodeList.item(j).setTextContent(nodeValue);
            } else {
                resolveLeafNodeValue(chileNode);
            }//ww w.  j  a  v  a  2  s.co  m
        }
    }
}

From source file:Main.java

public static String getContentText(Element elementNode) {
    StringBuffer result = new StringBuffer();
    NodeList children = elementNode.getChildNodes();
    for (int c = 0; c < children.getLength(); c++) {
        Node child = children.item(c);
        if ((child.getNodeType() == Node.TEXT_NODE) && (child instanceof Text)) {
            String name = ((Text) child).getData();
            result.append(name);/*from ww w  .  j  a  v a2 s .c o m*/
        }
    }
    return result.toString();
}

From source file:Main.java

public static String getTextData(Element element) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if ((childNodes.item(i).getNodeType() == Node.TEXT_NODE)
                || (childNodes.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) {
            if (childNodes.item(i) != null) {
                String tmpStr = ((Text) childNodes.item(i)).getWholeText().trim();
                if (tmpStr.length() > 0) {
                    return tmpStr;
                }//from   w w w .  j  av  a2s.  c  om
            }
        }
    }
    return null;
}

From source file:Main.java

public static Vector<Element> getChildList(Element elem, String elementName) {
    if (elem == null)
        return null;
    NodeList nl = elem.getChildNodes();
    if (nl == null)
        return null;
    Vector<Element> retlist = new Vector<Element>(100);
    for (int n = 0; n < nl.getLength(); n++) {
        Element element = (Element) nl.item(n);
        if (elementName.equals(element.getTagName())) {
            retlist.addElement(element);
        }/*from   w  ww .  java2  s.  co m*/
    }
    if (retlist.size() == 0)
        return null;
    return retlist;
}

From source file:Main.java

/**
 * Set the text content of an element. All exisitng Text Node are
 * removed before adding a new Text Node containing the given text.
 *
 * @param element target element to set text content, cannot be null.
 * @param text content of the element, cannot be null.
 *///from   www .j  ava  2s.  co  m
public static void setElementText(Element element, String text) {

    // Remove all text element
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            element.removeChild(n);
        }
    }
    Node child = element.getFirstChild();
    Node textnode = element.getOwnerDocument().createTextNode(text);

    // insert text node as first child
    if (child == null) {
        element.appendChild(textnode);
    } else {
        element.insertBefore(textnode, child);
    }
}

From source file:Main.java

public static String getText(Element node) {
    StringBuffer sb = new StringBuffer();
    NodeList list = node.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);

        switch (child.getNodeType()) {
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            sb.append(child.getNodeValue());
        }//from w w w .j a  va2  s. com
    }

    return sb.toString();
}

From source file:Main.java

/**
 * Get the first found child element with the provided local name that is a
 * direct child the provided element./* w  w w.j  a  v  a  2  s .  com*/
 * 
 * @param parent
 *            The parent element
 * @param name
 *            The name of the child element to find
 * @return The first found child element, null if no matching children
 */
public static Element getFirstChildElementByLocalName(Element parent, String localName) {
    assertNotNull(parent);
    NodeList children = parent.getChildNodes();
    Node node;
    for (int i = 0; i < children.getLength(); i++) {
        node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals(localName)) {
            return (Element) node;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Get child elements by classname//w ww  . j  a  v a2s.  co m
 *
 * @param ele parent element
 * @param cname of elements to find
 */
public static List<Element> getElementsByClass(Element ele, String cname) {
    Vector<Element> list = new Vector();
    NodeList nl = ele.getChildNodes();
    for (int j = 0; j < nl.getLength(); j++) {
        if (nl.item(j).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e = (Element) nl.item(j);
        if (e.getAttribute("class").equals(cname))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

/**
 * Access all immediate child elements inside the given Element
 *
 * @param element the starting element, cannot be null.
 * @param elemName the name of the child element to look for, cannot be
 * null./*from  ww  w. ja v a 2s  . com*/
 * @return array of all immediate child element inside element, or
 * an array of size zero if no child elements are found.
 */
public static Element[] getChildElements(Element element) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();
    ArrayList<Node> array = new ArrayList<Node>(len);

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            array.add(n);
        }
    }
    Element[] elems = new Element[array.size()];

    return (Element[]) array.toArray(elems);
}