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 String getElementText(Element elem) {
    StringBuilder buf = new StringBuilder();
    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  va2  s.c  om
            //TODO see jsf-samples
            //throw new FacesException("Unexpected node type " + n.getNodeType());
        }
    }
    return buf.toString();
}

From source file:Main.java

/** Helper method - Converts an XML <I>Element</I> to a <I>String</I> object, but
excludes the top level (wrapper) Element from the returned <I>String</I>.
   @param pElement An <I>org.w3c.dom.Element</I> object who's children are
  to be serialized to a <I>String</I>.
   @return <I>String</I> representation of the <I>Element</I>'s children.
 *//* www. j a  v a  2s .c o  m*/
public static String convertElementChildrenToString(Element pElement) {
    String strReturn = "";
    NodeList pChildren = pElement.getChildNodes();
    int nCount = pChildren.getLength();

    for (int i = 0; i < nCount; i++) {
        Node pChild = pChildren.item(i);

        if (pChild instanceof Element)
            strReturn += convertElementToString((Element) pChild);
    }

    return strReturn;
}

From source file:Main.java

public static List<Element> getChildElementsByName(Element root, String tagName) {
    List<Element> tags = new LinkedList<Element>();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);/*ww w.  java  2s  .c o m*/
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (tagName.equals(child.getNodeName())) {
            tags.add((Element) child);
        }
    }
    return tags;
}

From source file:Main.java

public static Element getElement(Element parentNode, String nodeName) {
    if (parentNode == null) {
        return null;
    }// ww  w.  ja  va 2s  .co m
    NodeList nl = parentNode.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
            if (nl.item(i).getNodeName().equals(nodeName)) {
                return (Element) nl.item(i);
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns true if element node has children elements
 * @param actElement//from   w  w w.  j  a  v a  2  s .  c o m
 * @return
 */
protected static boolean hasChildElementNodes(Element actElement) {
    try {
        if (actElement.hasChildNodes()) {
            NodeList childNodes = actElement.getChildNodes();

            for (int i = 0; i < childNodes.getLength(); i++) {
                Node element = childNodes.item(i);
                if (element.getNodeType() == Node.ELEMENT_NODE) {
                    return true;
                }
            }
        }
        return false;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static String getArgAttr(final Element node, final String strName, final String strAttrName) {
    return getArgAttr(node.getChildNodes(), strName, strAttrName);
}

From source file:Main.java

/**
 * This method will return the content of this particular <code>element</code>.
 * For example,//from w  ww  .j  a  v a  2s.c om
 * <p/>
 * <pre>
 *    <result>something_1</result>
 * </pre>
 * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
 * argument (<code>element</code> to this method, it returns the content of it,
 * namely, <code>something_1</code> in the example above.
 *
 * @return
 */
public static String getContent(Element element) {
    StringBuilder paramValue = new StringBuilder();
    NodeList childNodes = element.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node currentNode = childNodes.item(j);
        if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
            String val = currentNode.getNodeValue();
            if (val != null) {
                paramValue.append(val.trim());
            }
        }
    }
    return paramValue.toString().trim();
}

From source file:Main.java

public static String getValue(Element el) {
    if (el != null) {
        NodeList nodes = el.getChildNodes();
        StringBuffer sb = new StringBuffer();
        //       String s;
        int length = nodes.getLength();
        for (int i = 0; i < length; ++i) {
            Node node = nodes.item(i);
            String s = null;/*from ww w  . j  a  v  a  2s. c  om*/
            s = node.getNodeValue();
            //       System.out.println("XMLUtil.getValue: s=" + s);
            if (s != null)
                sb.append(s.trim());
        }
        if (sb.length() > 0) {
            if (debug) {
                System.out.println("XMLUtil.getValue: sb=" + sb.toString());
            }
            return sb.toString();
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildElements(final Element element) {
    final List<Element> elements = new ArrayList<Element>();
    final NodeList childs = element.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child instanceof Element) {
            elements.add((Element) child);
        }/*from ww w. j a v a 2  s.  co  m*/
    }
    return elements;
}

From source file:Main.java

public static List<Element> getChildElements(Element parent) {
    if (null == parent)
        return null;

    NodeList nodes = parent.getChildNodes();
    List<Element> elements = new ArrayList<Element>();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            elements.add((Element) node);
    }/*from   w  w w .  jav  a 2  s .c  o  m*/

    return elements;
}