Example usage for org.w3c.dom NodeList item

List of usage examples for org.w3c.dom NodeList item

Introduction

In this page you can find the example usage for org.w3c.dom NodeList item.

Prototype

public Node item(int index);

Source Link

Document

Returns the indexth item in the collection.

Usage

From source file:Main.java

private final static boolean isParentNode(Node element) {
    NodeList elements = element.getChildNodes();
    for (int i = 0; i < elements.getLength(); i++) {
        Node child = elements.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }/*from   w w w. j a  v a2s . c  om*/
    }

    return false;
}

From source file:Main.java

/**
 * Extract the text symbol from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes
 * into a single String symbol, excluding Comment nodes.
 * @see CharacterData//from   w  w  w  .  j  a va  2 s . c  om
 * @see EntityReference
 * @see Comment
 */
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);
        if (item instanceof CharacterData && !(item instanceof Comment) || item instanceof EntityReference)
            value.append(item.getNodeValue());
    }
    return value.toString();
}

From source file:Main.java

/**
 *  Returns the first child element found with the specified tag name
 *  @param node The node to search//from w  w w.  j  av a  2 s .c o m
 *  @param element The element tag name to search for
 *  @return The matching element Node
 */
public synchronized static Node getFirstElementWithTagName(Node node, String element) {
    if (node != null && element != null) {
        NodeList nl = node.getChildNodes();

        for (int i = 0; i < nl.getLength(); i++) {
            Node n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
                return n;
        }
    }

    return null;
}

From source file:Main.java

public static void removeContent(Element element) {
    NodeList nodeList = element.getChildNodes();
    while (nodeList.getLength() > 0) {
        element.removeChild(nodeList.item(0));
    }//from  ww  w. j av  a  2s .c o  m

}

From source file:Main.java

public static Element getFirstChild(Element parent, String name) {
    NodeList list = parent.getElementsByTagName(name);
    for (int i = 0; i < list.getLength(); i++) {
        Node child = list.item(i);
        if (child.getNodeType() == 1)
            return (Element) child;
    }/*from w  w w . j a v a 2  s.  c om*/

    return null;
}

From source file:Main.java

/**
 * Utility method that returns the first child element
 * identified by its getName.// w w  w  .  j a v a 2 s.  c  o  m
 * @param ele the DOM element to analyze
 * @param childName the child element getName to look for
 * @return the <code>org.w3c.dom.Element</code> creating,
 * or <code>null</code> if none found
 */
public static Element getChildElement(Element ele, String childName) {
    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && nodeNameEquals(node, childName))
            return (Element) node;
    }
    return null;
}

From source file:Main.java

public static Element getFirstChildElementIgnoreCase(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);
        if (node.getNodeType() == Node.ELEMENT_NODE
                && (name == null || node.getNodeName().equalsIgnoreCase(name)))
            return (Element) node;
    }// ww w .j a  v a  2 s  .  c o  m

    return null;
}

From source file:Main.java

/**
 * Given an node, returns the child text node of this element.
 * //from  w  w w . j  a  va  2  s .c  om
 * @param node
 *            the node to get the text node from
 * @return the text node that is a child of this node, or <CODE>null</CODE>
 *         if there is no such child
 */
public static String containedText(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node c = children.item(i);
        if (c.getNodeType() != Node.TEXT_NODE)
            continue;
        return ((Text) c).getData();
    }
    return null;
}

From source file:Main.java

public static boolean isSubTagExist(Node node, String tagName) {
    if (node == null) {
        return false;
    }//  w ww. j  ava 2 s.  c  om
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);
        if (n instanceof Element) {
            if (((Element) n).getTagName().equals(tagName)) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static Node selectNode(Node node, String tag) {
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);
        if (n instanceof Element && ((Element) n).getTagName().equals(tag)) {
            return n;
        }//from w w  w. j  a v  a2  s  .co  m
    }
    return null;
}