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

/**
 * Gets the text value of the given element.
 * //from   www  .j  a v a  2  s.c o  m
 * @param e the element.
 * @return the text contents of the given element.s
 */
public static String getText(Element e) {
    StringBuilder sb = null;

    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);

            switch (node.getNodeType()) {
            case Node.TEXT_NODE:
            case Node.CDATA_SECTION_NODE:
                if (sb == null) {
                    sb = new StringBuilder();
                }
                sb.append(node.getNodeValue());
                break;
            }
        }
    }

    return (sb != null) ? sb.toString() : null;
}

From source file:Main.java

/**
 * Vector of child elements of an element
 *//* w ww . j a v a2 s .  c  om*/
public static Vector<Element> childElements(Element el) {
    Vector<Element> res = new Vector<Element>();
    NodeList nodes = el.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node nd = nodes.item(i);
        if (nd instanceof Element) {
            Element eg = (Element) nd;
            res.addElement(eg);
        }
    }
    return res;
}

From source file:Main.java

public static List elements(Element element) {
    List elements = new ArrayList();
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if ((node instanceof Element) && (element == node.getParentNode())) {
            elements.add(node);// www. j  a va2s. c  o  m
        }
    }
    return elements;
}

From source file:Main.java

public static void getAllChildrenText(Element paramElement, String paramString,
        AbstractList paramAbstractList) {
    NodeList localNodeList = paramElement.getChildNodes();
    for (int i = 0; i < localNodeList.getLength(); i++) {
        Node localNode = localNodeList.item(i);
        if ((localNode.getNodeType() == 1) && (((Element) localNode).getTagName().equals(paramString))) {
            String str = getNodeText(localNode);
            if (str != null)
                paramAbstractList.add(str);
        }/*from  w  w w. ja v a 2  s . c  om*/
    }
}

From source file:Main.java

public static Element findFirstChild(Element elem, String tagName) {
    int i = findFirstChildIndex(elem, tagName);
    if (i >= 0) {
        return (Element) elem.getChildNodes().item(i);
    } else {/*from  w w w .  j  a  va2s  .  co m*/
        return null;
    }
}

From source file:Main.java

public static List<Element> getChildElements(Element element) {
    List<Element> elements = new LinkedList<Element>();

    NodeList children = element.getChildNodes();
    if (children == null) {
        return elements;
    }/* w ww . j av a 2  s . c o  m*/

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

        if (child.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) child);
        }
    }

    return elements;
}

From source file:Main.java

/**
 * Get the content of the given element.
 * @param element       The element to get the content for.
 * @param defaultStr    The default to return when there is no content.
 * @return              The content of the element or the default.
 *//*from   ww  w  .  j  av  a 2  s.  c  om*/
public static String getElementContent(Element element, String defaultStr) {
    if (element == null)
        return defaultStr;
    NodeList children = element.getChildNodes();
    String result = "";
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeType() == Node.TEXT_NODE
                || children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) {
            result += children.item(i).getNodeValue();
        }
    }
    return result.trim();
}

From source file:Main.java

public static String getTagValue(String tag, Element elem) {
    String retVal = "";
    NodeList fstNmElmntLst = elem.getElementsByTagName(tag);
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    if (fstNmElmnt == null)
        return "";
    NodeList fstNm = fstNmElmnt.getChildNodes();
    if (fstNm == null || fstNm.item(0) == null)
        return "";
    retVal = fstNm.item(0).getNodeValue();
    return retVal;
}

From source file:Main.java

public static String getElementText(Element elem) {
    StringBuffer buf = new StringBuffer();
    NodeList nodeList = elem.getChildNodes();
    for (int i = 0, len = nodeList.getLength(); i < len; i++) {
        Node n = nodeList.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            buf.append(n.getNodeValue());
        } else {//from   ww  w . j a v a2 s.  c  om
            //TODO see jsf-samples
            //throw new FacesException("Unexpected node type " + n.getNodeType());
        }
    }
    return buf.toString();
}

From source file:Main.java

static List<Element> childElements(Element memberNode) {
    final List<Element> list = new ArrayList<Element>();
    final NodeList childNodes = memberNode.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); ++i) {
        final Node childNode = childNodes.item(i);
        if (childNode instanceof Element) {
            list.add((Element) childNode);
        }/* ww w. ja v  a  2  s  .c o  m*/
    }
    return list;
}