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

/**
 * Return the contained text within an Element. Returns null if no text found.
 *///from   w w w  . ja  va  2 s.c o  m
public final static String getElementText(Element element) {
    NodeList nl = element.getChildNodes();

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

        if (c instanceof Text) {
            return ((Text) c).getData();
        }
    }

    return null;
}

From source file:Main.java

/**
 * Removes all Whitespace Noted from the specified element.
 * @param e Element/*from  w w w.j  ava  2  s  .  co  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

/**
 * Get the value of the specified element
 *
 * @param element// w  ww  .  j  a  v  a  2 s .c om
 * @return
 */
public static String getElementValue(Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
            return element.getFirstChild().getNodeValue();
        }
    }

    return null;
}

From source file:Main.java

/**Get a NodeLIst of child nodes, ignoring nodes that have White space
 * @param e//from w ww  .ja  v  a  2  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

/**
 * Scans the tag's children and returns the first text element found
 */// w  w  w  .  j  a va2s  .c o m
public static String getTagText(Element ele) {
    NodeList nl = ele.getChildNodes();
    int size = nl.getLength();

    Node node = null;
    int i = 0;
    for (; i < size; i++) {
        node = nl.item(i);
        if (node instanceof Text)
            break;
    }
    if (i == size || node == null)
        return null;

    return ((Text) node).getData();
}

From source file:Main.java

/**
 * Get all child nodes of a parent node/*from w  w  w.jav  a  2 s. c  o  m*/
 *
 * @param parent
 * @return
 */
public static NodeList getNodeList(Element parent) {
    return parent.getChildNodes();
}

From source file:Main.java

public static void reduceChildrenList(Element elem, Class clazz) {
    for (int i = 0; i < elem.getChildNodes().getLength();) {
        if (clazz.isAssignableFrom(elem.getChildNodes().item(i).getClass())) {
            ++i;//from  ww  w.jav  a2s  . com
        } else {
            elem.removeChild(elem.getChildNodes().item(i));
        }
    }
}

From source file:Main.java

/**
 * Gets the text value from the specified element.
 * @param e The element to get the text content from.
 * @return The text value of the element, or null if the tag is empty.
 *//*from   ww w.j  ava2  s  . c  om*/
private static String getTagValue(Element e) {
    Node n = (Node) e.getChildNodes().item(0);
    if (n == null)
        return null;

    return n.getNodeValue();
}

From source file:Main.java

public static void removeAllChilds(Element parentElem) {
    NodeList children = parentElem.getChildNodes();

    if (children != null) {
        int listLength = children.getLength();
        for (int i = listLength - 1; i >= 0; i--) {
            Element child = (Element) children.item(i);
            parentElem.removeChild(child);
        }//from w  w  w.  j a  v  a  2s. com
    }
}

From source file:Main.java

/**
 * Get a child element with a specific name.
 *
 * @param name Name of the requested child element.
 * @param elem Parent element.//from w w  w.  java2  s. c o  m
 *
 * @return The first child element with the given name, or null if none found.
 */
public static Element getChildElement(String name, Element elem) {

    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Element && n.getNodeName().equals(name))
            return (Element) n;
    }
    return null;
}