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 getNodeAttributeByName(String xmlFilePath, String nodeName, String attributeName) {
    String returnVal = "No value!";
    DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
    try {/*w ww.j av  a  2s. c  o m*/

        DocumentBuilder domBuilder = domBuilderFactory.newDocumentBuilder();
        InputStream is = new FileInputStream(xmlFilePath);
        Document doc = domBuilder.parse(is);
        Element root = doc.getDocumentElement();
        NodeList nodes = root.getChildNodes();
        if (nodes != null) {
            for (int i = 0; i < nodes.getLength(); i++) {
                Node node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    String test = node.getNodeName();
                    if (test.equals(nodeName)) {
                        returnVal = node.getAttributes().getNamedItem(attributeName).getNodeValue();
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return returnVal;
}

From source file:Main.java

public static Element getFirstChild(Element paramElement, String paramString) {
    NodeList localNodeList = paramElement.getChildNodes();
    for (int i = 0; i < localNodeList.getLength(); i++) {
        Node localNode = localNodeList.item(i);
        if ((localNode != null) && (localNode.getNodeType() == 1)
                && (((Element) localNode).getTagName().equals(paramString)))
            return (Element) localNode;
    }//from  w w  w  .  j  av a 2s  . c  om
    return null;
}

From source file:Main.java

/**
 * Returns the boolean value of the text content for the child element with the specified name
 * for the specified element./*from  ww w .  ja va2 s  .c  om*/
 *
 * @param element the parent element
 * @param name    the name of the child element to return
 *
 * @return the boolean value of the text content for the child element or <code>null</code> if a
 *         child element with the specified name could not be found
 */
public static Boolean getChildElementBoolean(Element element, String name) {
    NodeList nodeList = element.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);

        if (node instanceof Element) {
            Element childElement = (Element) node;

            if (childElement.getNodeName().equals(name)) {
                try {
                    return Boolean.parseBoolean(childElement.getTextContent());
                } catch (Throwable e) {
                    throw new RuntimeException("Failed to parse the invalid boolean value ("
                            + childElement.getTextContent() + ")");
                }
            }
        }
    }

    return null;
}

From source file:Main.java

public static List<Element> getChildElements(Element element) {
    List<Element> result = new ArrayList<Element>();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            result.add((Element) childNodes.item(i));
        }/* ww  w.  j a v a  2s.  c  o m*/
    }
    return result;
}

From source file:Main.java

protected static Element getChildElement(Element element, String tagName) {
    NodeList childNodes = element.getChildNodes();
    int numChildren = childNodes.getLength();

    for (int i = 0; i < numChildren; i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }/*from  w  w w . j a  va  2  s.c o  m*/
        Element childElement = (Element) childNode;

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

From source file:Main.java

public static Element getChildByTagName(Element element, String tagName) {
    Element result = null;/*from ww  w.  ja  va  2  s . c o  m*/
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n = childNodes.item(i);
        if (!(n instanceof Element)) {
            continue;
        }
        if (((Element) n).getTagName().equals(tagName)) {
            if (result != null) {
                throw new RuntimeException("Too many elements with tag name " + tagName);
            }
            result = (Element) n;
        }
    }
    return result;
}

From source file:Main.java

/**
 * Delete children elements for Element by element name
 *//*from  w  w w  . ja v a  2s.c o m*/
public static void removeAllChildren(Element node, String elementName) {
    if (node != null) {
        NodeList nl = node.getChildNodes();
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = nl.item(i);
            if (childNode != null && childNode.getLocalName() != null
                    && childNode.getLocalName().equals(elementName))
                node.removeChild(childNode);
        }
    }
}

From source file:Main.java

public static String getTextValue(Element valueEle) {

    StringBuffer value = new StringBuffer();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);/*  ww  w . j a v a 2  s. com*/
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            value.append(item.getNodeValue());
        }
    }
    return value.toString().trim();
}

From source file:Main.java

/**
 * Gets the immediately child elements list from the parent element.
 * // www  .  j  a  v a2  s . c  o m
 * @param parent the parent element in the element tree
 * @return the NOT NULL immediately child elements list
 */
public static List<Element> getChildElements(Element parent) {
    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 instanceof Element && node.getParentNode() == parent) {
            elements.add((Element) node);
        }
    }

    return elements;
}

From source file:Main.java

/**
 * Get the first child element of the specified element, null if it has no child elements.
 * /* w ww  . ja  v  a 2  s.  com*/
 * @param root The element to search.
 * @return The first child element, null if it has none.
 */
public static Element getFirstChild(Element root) {
    if (root == null)
        return null;
    NodeList lst = root.getChildNodes();
    final int n = lst.getLength();
    for (int i = 0; i < n; i++) {
        Node node = lst.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE)
            return (Element) node;
    }
    return null;
}