Example usage for org.w3c.dom NodeList getLength

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

Introduction

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

Prototype

public int getLength();

Source Link

Document

The number of nodes in the list.

Usage

From source file:Main.java

public static List<Node> getChildNodeList(Node parentNode, String nodeName) {
    List<Node> childNodeList = new ArrayList<>();
    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)) {
                childNodeList.add(childNode);
            }// w  w  w  . j ava2 s.  c  o  m
        }
    }
    return childNodeList;
}

From source file:Main.java

/**
 * Prints out the DOM tree.//from  w  w  w  . j a va  2 s  .c  o  m
 * 
 * @param n
 *            the parent node to start printing from
 * @param prefix
 *            string to append to output, should not be null
 */
public static void printDom(Node n, String prefix, StringBuilder sb) {
    String outString = prefix;
    if (n.getNodeType() == Node.TEXT_NODE) {
        outString += "'" + n.getTextContent().trim() + "'";
    } else {
        outString += n.getNodeName();
    }
    sb.append(outString);
    sb.append("\n");
    NodeList children = n.getChildNodes();
    int length = children.getLength();
    for (int i = 0; i < length; i++) {
        printDom(children.item(i), prefix + "  ", sb);
    }
}

From source file:Main.java

public static int getSizeNS(Element el, String nameSpaceURI, String tagName) {
    NodeList list = el.getElementsByTagNameNS(nameSpaceURI, tagName);
    return list.getLength();
}

From source file:Main.java

/**
 * Get the value of the attribute with the given name of the first tag found with the given tag name in the given
 * document<br/>./* www. java  2s.  c  o m*/
 *
 * @param doc
 * @param tagName
 * @param attributeName
 * @return the value of the attribute with the given name of the node or the empty string, if no node with this name
 * exits in this document or the attribute does not exist
 */
public static String getTagAttributeValue(Document doc, String tagName, String attributeName) {
    NodeList tagList = doc.getElementsByTagName(tagName);
    if (tagList.getLength() > 0) {
        NamedNodeMap attributes = tagList.item(0).getAttributes();
        if (attributes != null) {
            Node attribute = attributes.getNamedItem(attributeName);
            if (attribute != null) {
                return attribute.getNodeValue().trim();
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * Extracts the text from the given element.
 * Element.getTextContet() is java5 specific, so we need to use this until we drop 1.4 support.
 *///w  w  w .j  av a 2s .c om
public static String getTextContent(Node element) {
    StringBuffer text = new StringBuffer();
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node child = childNodes.item(i);
        if (child instanceof Text) {
            text.append(child.getNodeValue());
        }
    }

    return text.toString();
}

From source file:Main.java

public static String getNodesText(NodeList nodes) {
    StringBuffer ret = new StringBuffer();
    for (int i = 0, l = nodes.getLength(); i < l; i++) {
        ret.append(nodes.item(i).getTextContent());
    }/* w w w .  j a va2s  .c om*/
    return ret.toString();
}

From source file:Main.java

public static String getChildNodeText(Node root, String childName) {
    NodeList nodeList = root.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        if (childName.equals(nodeList.item(i).getNodeName()))
            return nodeList.item(i).getTextContent();
    }//from w  w  w  . j a v  a2s . c o m
    return "";
}

From source file:Main.java

public static Element getElement(Element parentElement, String nodeName) {
    NodeList list = parentElement.getElementsByTagName(nodeName);
    if (list == null || list.getLength() == 0) {
        return null;
    }/*from  w  w  w. j ava 2s  .c om*/
    return (Element) list.item(0);
}

From source file:Main.java

/**
 * Check if the given Element contains child nodes that match with the given
 * child name.// w  w  w . j  a  v  a  2s. c  o  m
 * 
 * @param element
 * @param name The name of the child tag to match on. The special value "*"
 *        matches all tags.
 * @return
 */
public static boolean hasChildNodes(Element element, String name) {
    NodeList elements = element.getElementsByTagName(name);
    return elements.getLength() > 0;
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String nodeName) {
    Node node = null;//from w w w  .  j  av  a 2  s  .co  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;
}