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 Element getLocalElementByTagName(Element context, String tagName) {
    NodeList childNodes = context.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node instanceof Element && tagName.equals(node.getNodeName()))
            return (Element) node;
    }/*from  w  w  w  .j ava 2s .  co  m*/
    return null;
}

From source file:Main.java

/** Gets the text information associated with the given DOM element. */
public static String getText(final Element el) {
    final NodeList nodes = el.getChildNodes();
    final int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        final Node node = nodes.item(i);
        if ("#text".equals(node.getNodeName()))
            return node.getNodeValue();
    }// w  w  w  .  ja  v  a  2s.com
    return null;
}

From source file:Main.java

public static String getSingleValue(Element e, String s) {
    String value = null;// w w w.  ja v a2 s.  c  om
    NodeList nodeList = e.getElementsByTagName(s);
    if (nodeList.getLength() > 0) {
        Element _e = (Element) nodeList.item(0);
        NodeList eValue = _e.getChildNodes();
        if (eValue.getLength() > 0) {
            value = eValue.item(0).getNodeValue();
        }
    }

    return value;
}

From source file:Main.java

/**
 * Returns a list with the elements included in the childrens of element
 * that have name = tag./*  w  ww .  j  av  a 2 s  .  c o  m*/
 * @param element Element
 * @param tag name of the Element.
 * @return List with the indicated elements.
 */
public static List<Element> childsTags(Element element, String tag) {
    NodeList nl = element.getChildNodes();

    ArrayList<Element> ret = new ArrayList<Element>();

    if (nl != null) {
        int length = nl.getLength();

        for (int i = 0; i < length; i++) {
            if (tag.equals((nl.item(i)).getNodeName())) {
                ret.add((Element) nl.item(i));
            }
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Gets tag value from XML entry.//from   ww  w.  j  a v  a2  s . c  om
 * @param tag         XML tag String.
 * @param xmlElement   XML Element Object.
 * @return            Integer.parseInt(((Node) lastNodeList.item(0)).getNodeValue()).
 */
public static String getEntry(String tag, Element xmlElement) {
    NodeList nodeList = xmlElement.getElementsByTagName(tag);
    Element element = (Element) nodeList.item(0);
    NodeList lastNodeList = element.getChildNodes();
    return lastNodeList.item(0).getNodeValue();
}

From source file:Main.java

public static String getBody(Element element) {
    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() == 0)
        return null;

    Node firstNode = nodes.item(0);
    if (firstNode == null)
        return null;

    return firstNode.getNodeValue();
}

From source file:Main.java

public static Element getChild(Element element, String tagName) {
    NodeList nodes = element.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node instanceof Element) {
            Element child = (Element) node;
            if (child.getTagName().equals(tagName))
                return child;
        }//from   ww w . j a va2  s . c  o m
    }
    return null;
}

From source file:Main.java

public static Element getFirstChildElementByName(Element root, String tagName) {
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);//from ww w. j  a v a  2 s  .  c om
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if (tagName.equals(child.getNodeName())) {
            return (Element) child;
        }
    }
    return null;
}

From source file:Main.java

/**
 * /*  w  w w  .ja v  a 2 s  .c o  m*/
 * @param context
 * @param element
 */
public static void recursiveIdBrowse(DOMValidateContext context, Element element) {
    for (int i = 0; i < element.getChildNodes().getLength(); i++) {
        Node node = element.getChildNodes().item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element childEl = (Element) node;
            String ID_ATTRIBUTE_NAME = "Id";
            if (childEl.hasAttribute(ID_ATTRIBUTE_NAME)) {
                context.setIdAttributeNS(childEl, null, ID_ATTRIBUTE_NAME);
            }
            recursiveIdBrowse(context, childEl);
        }
    }
}

From source file:Main.java

public static ArrayList<Node> getChildNodesWithTagName(String tagName, Element parentElement) {
    NodeList childNodes = parentElement.getChildNodes();

    ArrayList<Node> childNodesWithTagName = new ArrayList<Node>();

    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE && childNode.getNodeName().equals(tagName)) {
            childNodesWithTagName.add(childNode);
        }// ww w.  j  a  v  a2 s.  co  m
    }

    return childNodesWithTagName;
}