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

/**
 * Returns all children of the given element which are element named as
 * specified./* w w w . j  a v a  2  s. c o m*/
 */
public static List<Element> getNamedChildren(Element element, String elementNames) {
    List<Element> result = new ArrayList<Element>();
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(elementNames)) {
            result.add((Element) node);
        }
    }
    return result;
}

From source file:Main.java

public static Hashtable<String, String> getChildHash(Element elem) {
    if (elem == null)
        return null;
    NodeList nl = elem.getChildNodes();
    if (nl == null)
        return null;
    Hashtable<String, String> retlist = new Hashtable<String, String>(nl.getLength());
    for (int n = 0; n < nl.getLength(); n++) {
        Node child = nl.item(n);/* w  w w  .j a va 2  s . c om*/
        if (child instanceof Element) {
            Element element = (Element) child;
            String key = element.getTagName();
            String value = getSimpleElementText(element);
            retlist.put(key, value);
        }
    }
    if (retlist.size() == 0)
        return null;
    return retlist;
}

From source file:Main.java

/**
 * Retrieves the given DOM element's child elements with the specified name.
 * If name is null, all children are retrieved.
 *///from   w  ww  .  j  a  va 2s  . c  om
public static Element[] getChildren(final Element el, final String name) {
    final Vector v = new Vector();
    final NodeList nodes = el.getChildNodes();
    final int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        final Node node = nodes.item(i);
        if (!(node instanceof Element))
            continue;
        final Element e = (Element) node;
        if (name == null || e.getTagName().equals(name))
            v.add(e);
    }
    final Element[] els = new Element[v.size()];
    v.copyInto(els);
    return els;
}

From source file:Main.java

public static Element getFirstChildElement(Element elm, String name) {
    if (elm == null)
        return null;

    NodeList nl = elm.getChildNodes();
    for (int c = 0; c < nl.getLength(); c++) {
        Node node = nl.item(c);//from  w w w.  j av  a 2  s. c o  m
        if (node.getNodeType() == Node.ELEMENT_NODE && (name == null || node.getNodeName().equals(name)))
            return (Element) node;
    }

    return null;
}

From source file:Main.java

/**
 * Get the elements with the given name.
 * @param element/* w  ww  .j av a2 s  .  c o  m*/
 * @param name
 * @return array, never <code>null</code>
 */
public static Element[] getChildElementsNamed(Element element, String name) {
    if (element == null)
        return new Element[0];
    NodeList nodeList = element.getChildNodes();
    List<Element> elements = new ArrayList<Element>();
    Node node = nodeList.item(0);
    while (node != null) {
        if (node instanceof Element && name.equals(node.getNodeName())) {
            elements.add((Element) node);
        }
        node = node.getNextSibling();
    }
    return (Element[]) elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

/**
 * Get child elements by name, ignoring namespace declarations
 *
 * @param ele parent element// w  ww . ja  v  a  2  s .co  m
 * @param name name of elements to find
 */
public static List<Element> getElementsByName(Element ele, String name) {
    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);
        String n = e.getNodeName();
        if (matches(n, name))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

/**
 * Returns the number of child elements of parent that have the given
 * element tag name.//from w w  w  .j av  a  2s . c om
 * 
 * @param name
 *            Element tag name to search for
 * @param parent
 *            Parent to search
 * @return Count of child elements that have the given name
 */
public static int getCountChildElementsByTagName(String name, Element parent) {
    if (parent == null) {
        return 0;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return 0;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(name)) {
                count++;
            }
        }
    }
    return count;
}

From source file:Main.java

public static List<Element> getChildElementsByTagName(Element ele, String... childEleNames) {
    List<String> childEleNameList = Arrays.asList(childEleNames);
    NodeList nl = ele.getChildNodes();
    List<Element> childEles = new ArrayList<Element>();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);/*from  ww  w  .j  a va 2 s  .  c  om*/
        if (node instanceof Element && nodeNameMatch(node, childEleNameList)) {
            childEles.add((Element) node);
        }
    }
    return childEles;
}

From source file:Main.java

public static String getTextContents(Element e) {
    if (!e.hasChildNodes())
        return null;

    StringBuffer buf = new StringBuffer();
    NodeList list = e.getChildNodes();
    int len = list.getLength();
    String text;/*from w  w w.  j  a va 2 s  . com*/
    for (int i = 0; i < len; i++) {
        text = ((Node) list.item(i)).getNodeValue();
        if (text != null)
            buf.append(text);
    }
    return buf.toString();
}

From source file:Main.java

public static void emptyXMLDocument(File file) {
    if (file.exists()) {
        Document document = loadXMLDocument(file);
        if (document != null) {
            Element root = getRoot(document);
            NodeList nodeList = root.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                root.removeChild(nodeList.item(i));
            }// w ww  . j a  va  2s . co m
            saveXMLDocument(document, file);
        }
    }
}