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

/**
 * Gets the text value contained in an element.
 *
 * @param node The Element to get the text value of.
 *
 * @return The text in the element./* w  w  w  .  j  a v  a2  s . c om*/
 */
public static String getTextValue(Element node) {
    if (node == null) {
        return null;
    }
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n.getNodeType() == n.TEXT_NODE) {
            return n.getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

public static void removeElement(Element parent, String tagName) {
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " begin...");
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nd = nl.item(i);//from   w ww .  j  av a  2s  .co  m
        if (nd.getNodeName().equals(tagName)) {
            parent.removeChild(nd);
            logger.debug("remove child '" + nd + "' success.");
        }
    }
    logger.debug("remove " + parent.getNodeName() + "'s children by tagName " + tagName + " end.");
}

From source file:Main.java

/**
 * This method will return the content of this particular <code>element</code>.
 * For example,/*from  w ww.j  ava 2s .  c o m*/
 *
 * <pre>
 *    <result>something_1</result>
 * </pre>
 * When the {@link org.w3c.dom.Element} <code>&lt;result&gt;</code> is passed in as
 * argument (<code>element</code> to this method, it returns the content of it,
 * namely, <code>something_1</code> in the example above.
 * 
 * @return
 */
public static String getContent(Element element) {
    StringBuffer paramValue = new StringBuffer();
    NodeList childNodes = element.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node currentNode = childNodes.item(j);
        if (currentNode != null && currentNode.getNodeType() == Node.TEXT_NODE) {
            String val = currentNode.getNodeValue();
            if (val != null) {
                paramValue.append(val.trim());
            }
        }
    }
    String val = paramValue.toString().trim();
    return val;
}

From source file:Main.java

@Deprecated
private static void removeWhitespaceNodes(org.w3c.dom.Element e) {
    org.w3c.dom.NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        org.w3c.dom.Node child = children.item(i);
        if ((child instanceof org.w3c.dom.Text)
                && (((org.w3c.dom.Text) child).getData().trim().length() == 0)) {
            e.removeChild(child);//from w w w . j  av  a 2s.c  o  m
        } else if (child instanceof org.w3c.dom.Element) {
            removeWhitespaceNodes((org.w3c.dom.Element) child);
        }
    }
}

From source file:Main.java

/**
 * Gets the child elements of a parent element. Unlike DOM's getElementsByTagName, this does no recursion,
 * uses local name (namespace free) instead of tag name, result is a proper Java data structure and result
 * needs no casting. In other words, this method does not suck unlike DOM.
 * /* www  .  ja v  a 2 s  .  c  om*/
 * @param parent the XML parent element
 * @param name name of the child elements, if null then all are returned
 */
public static List<Element> getChildElements(Element parent, String name) {
    List<Element> childElements = new ArrayList<Element>();
    NodeList childNodes = parent.getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {
        // get elements
        if (childNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {

            // match element name
            Element childElement = (Element) childNodes.item(i);
            if (name == null || childElement.getLocalName().equals(name)) {
                childElements.add(childElement);
            }
        }
    }

    return childElements;
}

From source file:Main.java

public static Collection<Element> getChilds(Element e) {
    List<Element> childs = new ArrayList<Element>();
    if (e == null)
        return childs;

    NodeList cnodes = e.getChildNodes();
    for (int i = 0; i < cnodes.getLength(); i++) {
        Node cnode = cnodes.item(i);
        if (cnode instanceof Element) {
            Element child = (Element) cnode;
            childs.add(child);/*from w w w  .  j a va2  s .  co  m*/
        }
    }
    return childs;
}

From source file:nl.igorski.lib.utils.data.XMLTool.java

/**
 * get the value of a specific tag in an Element
 *
 * @param element {Element}//from  w  w  w. jav  a2 s. c  om
 * @param tagName {String} name of the tag
 * @return {String} the tags value
 */
public static String getTagValue(Element element, String tagName) {
    try {
        NodeList list = element.getElementsByTagName(tagName);
        Element theElement = (Element) list.item(0);

        NodeList textList = theElement.getChildNodes();
        return textList.item(0).getNodeValue().trim();
    } catch (Exception e) {
    }

    return "";
}

From source file:Main.java

/**
 * An Iterable for the Element's childs, with a particular name, of a node
 * /*  www.j av a  2  s.com*/
 * @param n
 *            the node get the children from
 * @param elementName
 *            the name of the child elements
 * @return An Iterable for the Element's children, with a particular name, of a node
 */
public static List<Element> elements(Element n, String elementName) {
    //        NodeList subNodes = n.getElementsByTagName(elementName);
    NodeList subNodes = n.getChildNodes();
    int sz = subNodes.getLength();
    ArrayList<Element> elements = new ArrayList<Element>(sz);
    for (int idx = 0; idx < sz; idx++) {
        Node node = subNodes.item(idx);
        if (node instanceof Element) {
            Element el = (Element) node;
            if (el.getLocalName().equals(elementName))
                elements.add((Element) node);
        }
    }
    return elements;
}

From source file:Main.java

private static Node getChildNodeByType(Element element, short nodeType) {
    if (element == null) {
        return null;
    }/*from   ww  w  . ja v a  2s . com*/

    NodeList nodes = element.getChildNodes();
    if (nodes == null || nodes.getLength() < 1) {
        return null;
    }

    Node node;
    String data;
    for (int i = 0; i < nodes.getLength(); i++) {
        node = nodes.item(i);
        short type = node.getNodeType();
        if (type == nodeType) {
            if (type == Node.TEXT_NODE || type == Node.CDATA_SECTION_NODE) {
                data = ((Text) node).getData();
                if (data == null || data.trim().length() < 1) {
                    continue;
                }
            }

            return node;
        }
    }

    return null;
}

From source file:Main.java

/**
 * Looks for a text child node and returns its value.
 *
 * @param tag - XML element/*from  w  ww  . jav  a 2  s  . c  om*/
 * @return - the text String of the tag
 */
public static String getText(final Element tag) {
    if (tag == null)
        return null;

    NodeList lst = tag.getChildNodes();
    StringBuffer buf = new StringBuffer();

    for (int Index = 0, Cnt = lst.getLength(); Index < Cnt; Index++) {
        if (lst.item(Index).getNodeType() == Node.ENTITY_REFERENCE_NODE) {
            buf.append(lst.item(Index).getChildNodes().item(0).getNodeValue());
        }
        if ((lst.item(Index).getNodeType() == Node.TEXT_NODE)
                || (lst.item(Index).getNodeType() == Node.CDATA_SECTION_NODE)) {
            buf.append(lst.item(Index).getNodeValue());
        }
    }

    if (buf.length() == 0)
        return null;
    else
        return buf.toString();
}