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

/**
 * Determine whether a single child is available of a particular type.
 * @return true if the specified child element is present
 * @throws Exception if the child is present multiple times.
 *///from ww  w  . j a  v a2 s . c  o  m
public static boolean hasChild(Element element, String child) throws Exception {
    NodeList nodes = element.getChildNodes();
    Element ret = null;
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode.getNodeName().equals(child) && childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (ret != null) {
                throw new Exception("Child element '" + child + "' present multiple times");
            } else {
                ret = (Element) childNode;
            }
        }
    }
    return ret != null;
}

From source file:Main.java

public static String getText(Element node) {

    NodeList children = node.getChildNodes();

    if (0 < children.getLength()) {

        String value = children.item(0).getNodeValue();

        if (value != null) {
            return value.trim();
        }// w  w  w  .  j  av a  2  s.c o  m
    }

    return null;
}

From source file:Main.java

public static String[] getChildrenText(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<String> list = new LinkedList<String>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);//w ww .  jav  a2s .c o  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(getText((Element) n));
        }
    }
    return list.toArray(new String[list.size()]);
}

From source file:Main.java

/**
 * Extracts from the given Element the first child having the given name.
 *
 * @param element/*w ww . ja v  a 2 s .co  m*/
 * @param name
 * @return the first child with the given name or null if none
 */
public static Element firstChild(Element element, String name) {
    NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node current = nodeList.item(i);
        if (current.getNodeType() == Node.ELEMENT_NODE && current.getNodeName().equals(name)) {
            return (org.w3c.dom.Element) current;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get the child elements of the specified element that match the tags passed in.
 * @param element - the element who's children we wish to search
 * @param tags - the tags to match (empty returns all child elements).
 * @return The child elements that matched
 *///from ww  w.  ja  va 2 s  .c om
public static List<Element> childElements(Element element, String... tags) {
    NodeList nodeList = element.getChildNodes();
    return getElements(nodeList, tags);
}

From source file:Main.java

/**
 * Returns the text-content of the first element in a document with a given tag name.
 * This is useful for well structured documents when it is known that there is only
 * one such element./*  w  w w  . j  av a2 s .com*/
 * 
 * @param document The document to search within.
 * @param tagname The name of the element to retrieve.
 * @return The text content of the element. Text nodes therein are appended for the result, but
 * no further descendants are included. If the element could not be found, the empty string is
 * returned.
 */
public static String getNodeContent(Document document, String tagname) {
    NodeList list = document.getElementsByTagName(tagname);
    if (list.getLength() < 1) {
        //            log.debug("Not found: " + tagname);
        return "";
    }
    Element tag = (Element) list.item(0);
    NodeList content = tag.getChildNodes();
    StringBuffer buf = new StringBuffer();
    for (int i = 0; i < content.getLength(); i++) {
        Node node = content.item(i);
        if (node instanceof Text) {
            buf.append(((Text) node).getNodeValue());
        }
    }
    String textcontent = buf.toString().trim();
    //        log.debug("getNodeContent: " + tagname + " = [" + textcontent + "]");
    return textcontent;
}

From source file:Main.java

/**
 * Get only the XML element children of an XML element.
 *
 * @param parent/*from   ww  w  . j av a 2s.  c  om*/
 *            The parent element to get children from
 * @return The list of children Element of the parent
 */
public static List<Element> getChildElements(Element parent) {
    NodeList childNodes = parent.getChildNodes();
    List<Element> childElements = new ArrayList<>();
    for (int index = 0; index < childNodes.getLength(); index++) {
        if (childNodes.item(index).getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) childNodes.item(index));
        }
    }
    return childElements;
}

From source file:Main.java

/**
 * This method return role name from xml file.
 * @param element String- privilege Element
 * @param elementName -Element name for which value has to be return
 * @return String Role name/* w  ww. j  a  va2  s  .  c  om*/
 */
public static String getElementValue(Element element, String elementName) {
    String roleName = "";
    NodeList elementList = element.getElementsByTagName(elementName);
    Element ele = (Element) elementList.item(0);
    NodeList valueNodeList = ele.getChildNodes();
    Node node = ((Node) valueNodeList.item(0));
    if (node != null) {
        roleName = node.getNodeValue();
    }
    return roleName;
}

From source file:Main.java

public static String findNodeValue(Element firstElement, String name) {
    String nodeValue = null;/*  w  w  w.  jav a2s .  co m*/
    NodeList firstNameList = firstElement.getElementsByTagName(name);
    Element firstNameElement = (Element) firstNameList.item(0);

    NodeList textFNList = firstNameElement.getChildNodes();

    if (textFNList.getLength() > 0) {
        nodeValue = (textFNList.item(0)).getNodeValue();
    }
    return nodeValue;
}

From source file:Main.java

public static Element getLastChild(Element parent, String name) {
    Element child = null;/*  ww  w  .j a  v  a 2  s  .c  o  m*/
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            child = (Element) n;
        }
    }
    return child;
}