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

public static String getNormalizedText(Node node) {
    String res = "";
    if (node.hasChildNodes()) {
        NodeList nl = node.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
                res += nl.item(i).getNodeValue();
            } else {
                // ignore <SCRIPT> nodes ...
                if (!nl.item(i).getNodeName().equalsIgnoreCase("script"))
                    res += getNormalizedText(nl.item(i));
            }/*w ww  .  j  a v  a  2 s.c  o m*/
        }
    }
    return res;
}

From source file:Main.java

/**
 * Finds the first child of a node with the given name
 * @param parent the parent node/*from   w  w  w .j a  v a  2 s.  co  m*/
 * @param name the name
 * @return the child node
 */
public static Node findFirstChild(Node parent, String name) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (name.equals(node.getNodeName())) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

public static String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE)
                    return data.getNodeValue();
            }/*  w  ww . ja  v  a  2 s. com*/
        }
    }
    return "";
}

From source file:Main.java

public static String getTagValue(Node node, String name) {

    if (node.getNodeType() == Node.ELEMENT_NODE) {

        Element el = (Element) node;
        NodeList nodeList = el.getElementsByTagName(name);
        if (nodeList.getLength() > 0) {
            el = (Element) nodeList.item(0);
            nodeList = el.getChildNodes();

            if (nodeList.getLength() > 0) {
                return ((Node) nodeList.item(0)).getNodeValue();
            }//from   w  w  w .j a v a 2  s .  co m
        }

    }
    return "";
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String nodeName) {
    Node node = null;/*from   ww  w  . j a  va2s  . c  o m*/
    NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
            if (((Element) childNode).getNodeName().equals(nodeName)) {
                node = childNode;
                break;
            }
        }
    }
    return node;
}

From source file:Main.java

private static String getNodeName(Node node) {
    String name = "";
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node curNode = nodeList.item(i);
        if ("name".equalsIgnoreCase(curNode.getNodeName())) {
            name = curNode.getTextContent().replace("\"", "");
            break;
        }/*from  ww w  .j  a  v a 2 s.c  o  m*/
    }

    return name;
}

From source file:Main.java

/**
 * Determines the first child node of the parent with the specified tag
 * name.//from w w  w .  ja v  a2 s . c om
 * 
 * @param parent   The parent node of the child node to be determined.
 * @param nodeName The name of the child node to be determined.
 * 
 * @return The determined child node with the given name.
 */
public static Node getChildWithName(Node parent, String nodeName) {
    if (parent != null) {
        NodeList childs = parent.getChildNodes();
        for (int i = 0; i < childs.getLength(); i++) {
            Node child = childs.item(i);
            if ((child.getLocalName() != null) && child.getLocalName().equals(nodeName)) {
                return childs.item(i);
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get sub tag content.//  w  w w. j  a  va2 s .c o m
 * @param node to process
 * @param tagName the tag name
 * @return the child content
 */
public static String getSubTagValue(Node node, String tagName) {
    if (node == null) {
        return null;
    }
    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)) {
                Node nn = n.getFirstChild();
                if (nn instanceof Text) {
                    return ((Text) nn).getData();
                }
            }
        }
    }
    return null;
}

From source file:Main.java

public static Element getChildElement(Element el, String tagName) {
    if (el == null)
        return null;
    NodeList list = el.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        return (Element) list.item(0);
    }//from  w  ww  .  j  a  va  2s  . c  o  m
    return null;
}

From source file:Main.java

private static void removeWhitespaces(Element element) {
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength();) {
        Node n = children.item(i);
        if (n instanceof Element) {
            removeWhitespaces((Element) n);
            ++i;//from   w  w  w  .  j av a 2s  .c  o m
        } else if (n instanceof Text && ((Text) n).getTextContent().trim().length() == 0) {
            element.removeChild(n);
        } else {
            ++i;
        }
    }
}