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

/**
 * Obtains the first child element with the specified name inside
 * the specified namespace./*from www  .  ja v  a 2  s .c om*/
 *
 * @param element the root element.
 * @param namespace the namespace of the child element.
 * @param tagName the child local name.
 * @return the child element.
 */
public static Element getChildElement(final Element element, final String namespace, final String tagName) {
    final NodeList childNodes = element.getChildNodes();
    final int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        final Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        final Element childElement = (Element) childNode;
        String childTagName = childElement.getTagName();
        final String childPrefix = childElement.getPrefix();
        final String childNamespace = (childPrefix != null ? childElement.lookupNamespaceURI(childPrefix)
                : null);

        if (namespace != null) {
            if (!namespace.equals(childNamespace)) {
                continue;
            } else {
                childTagName = childElement.getLocalName();
            }
        }

        if (!childTagName.equals(tagName)) {
            continue;
        }
        return childElement;
    }
    return null;
}

From source file:Main.java

/**
 * Looks up a child with the given (local) name.
 * <p>//  w w w.j av a  2 s.  c om
 * An array of several names may be passed, in which case they will be traversed in a simple
 * XPath-like fashion.
 */

public static Element getChildNamed(Element element, String... names) {

    if (element == null) {
        return null;
    }

    Element child = null;
    NodeList children = element.getChildNodes();

    outer: for (String name : names) {
        for (int loop = 0, length = children.getLength(); loop < length; loop++) {
            Node node = children.item(loop);

            if (!(node instanceof Element)) {
                continue;
            }

            child = (Element) node;

            if (name.equals(child.getLocalName())) {
                children = child.getChildNodes();
                continue outer;
            }
        }

        // No match found

        return null;
    }

    return child;
}

From source file:Main.java

static public Element selectSingleElement(Element element, String xpathExpression)
        throws XPathExpressionException {
    if (xpathExpression.indexOf("/") == -1) {
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(xpathExpression)) {
                return (Element) node;
            }/* w  ww .  j a v a  2  s  .  co  m*/
        }
        //  NodeList nodes = element.getElementsByTagName(xpathExpression);
        //  if (nodes.getLength() > 0) {
        //      return (Element) nodes.item(0);
        //  } else {
        return null;
        //  }
    } else {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element node = (Element) xpath.evaluate(xpathExpression, element, XPathConstants.NODE);
        return node;
    }
}

From source file:Main.java

public static String getFullTextFromChildren(Element element) {
    if (element == null) {
        return null;
    }/*ww  w . j  av a 2s.c  om*/

    StringBuffer sb = new StringBuffer(1000);
    NodeList nl = element.getChildNodes();
    Node child = null;
    int length = nl.getLength();

    for (int i = 0; i < length; i++) {
        child = nl.item(i);

        if (child.getNodeType() == Node.TEXT_NODE) {
            sb.append(child.getNodeValue());
        }
    }

    return sb.toString().trim();
}

From source file:Util.java

/**
 * Renvoie les lments enfants (uniquement de type ELEMENT)
 * /*  w ww .j av a  2s .  co m*/
 * @param e
 * @return
 */
public static Iterable<Element> getChildElements(final Element e) {
    return new Iterable<Element>() {
        public Iterator<Element> iterator() {
            final NodeList list = e.getChildNodes();
            int i = 0;
            for (; i < list.getLength(); i++) {
                if (list.item(i).getNodeType() == Node.ELEMENT_NODE)
                    break;
            }
            final int init = i;
            return new Iterator<Element>() {
                int cur = init;

                public void remove() {
                    throw new UnsupportedOperationException();
                }

                public Element next() {
                    Element item = (Element) list.item(cur);
                    for (cur++; cur < list.getLength(); cur++) {
                        if (list.item(cur).getNodeType() == Node.ELEMENT_NODE)
                            break;
                    }
                    return item;
                }

                public boolean hasNext() {
                    return cur < list.getLength();
                }

            };
        }
    };
}

From source file:Main.java

/**
 * get the  text string in an element (eg interspersed between child elements), 
 * or "" if there is none or if the Element is null.
 * Tries to ignore white space text; but does not succeed.
 *//*from   w  ww .j  ava  2  s  . c o  m*/
public static String getText(Element el) {
    String res = "";
    if (el != null)
        try {
            el.normalize(); // does not help recognise white space
            NodeList nodes = el.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++)
                if (nodes.item(i) instanceof Text) {
                    Text text = (Text) nodes.item(i);
                    // this filter seems to make no difference
                    if (!text.isElementContentWhitespace()) {
                        String tData = text.getData();
                        // this seems to be an effective way to catch pure white space
                        StringTokenizer nonWhiteSpace = new StringTokenizer(tData, "\n \t");
                        if (nonWhiteSpace.countTokens() > 0)
                            res = res + tData;
                    }
                }
        } catch (Exception e) {
            System.out.println("Text failure: " + e.getMessage());
        }
    return res;
}

From source file:Main.java

/**
 * Access an immediate child element 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./*w ww . ja  va 2 s.c  o m*/
 * @return the first immediate child element inside element, or
 * <code>null</code> if the child element is not found.
 */
public static Element getChildElement(Element element, String elemName) {
    NodeList list = element.getChildNodes();
    int len = list.getLength();

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

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            if (elemName.equals(n.getNodeName())) {
                return (Element) n;
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element fetchSubElement(Element e, String subElementName) {
    if (e == null || subElementName == null) {
        return null;
    }/*  ww w  .  j  a  v a 2 s  .c om*/

    subElementName = subElementName.toUpperCase();

    NodeList list = e.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i) instanceof Element) {
            Element element = (Element) list.item(i);
            if (element.getTagName().toUpperCase().equals(subElementName)) {
                return element;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * This method searches children of Element element for element with tagName
 * and namespaceURI nsName. It searchs one level down only.
 * //from  w w  w  . j ava  2  s .c  o  m
 * @param element
 *            The root element
 * @param nsName
 *            NamespaceURI
 * @param tagName
 *            A String representing the name of the tag to be searched for.
 * @return A List of elements that meet the criterial.
 */
public static List getElementsByTagNameNS1(Element element, String nsName, String tagName) {
    List list = new ArrayList();

    if (element != null) {
        NodeList nl = element.getChildNodes();
        int length = nl.getLength();
        for (int i = 0; i < length; i++) {
            Node child = nl.item(i);
            String childName = child.getLocalName();
            String childNS = child.getNamespaceURI();
            if ((childName != null) && (childName.equals(tagName)) && (childNS != null)
                    && (childNS.equals(nsName))) {
                list.add(child);
            }
        }
    }
    return list;
}

From source file:Main.java

/**
 * Removes the 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   w  w  w  . j av  a  2 s. com
 * @return the removed child element inside element, or
 * <code>null</code> if the child element is not found.
 */
public static Element removeChildElement(Element element, String elemName) {
    NodeList list = element.getChildNodes();
    Element cur = element;
    int len = list.getLength();
    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n != null) {
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getNodeName())) {
                    cur = (Element) element.removeChild(n);
                }
            }
        }
    }
    return cur;
}