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

/**
 * Get the attribute value of a given attribute name for
 * the first XML {@code org.w3c.dom.Element} of given name.
 *
 * @param elem the parent XML Element/*from  w w  w .j a va 2 s.c  om*/
 * @param name the name of the child text Element
 * @param attrName the attribute name
 * @return attribute value of named child Element
 */
public static String getFirstAttribute(Element elem, String name, String attrName) {
    NodeList nodeList = elem.getElementsByTagName(name);
    if (nodeList.getLength() == 0) {
        return null;
    }

    return (((Element) nodeList.item(0)).getAttribute(attrName));
}

From source file:Main.java

public static void printNodeList(NodeList list) {
    for (int i = 0; i < list.getLength(); i++) {
        printNode(list.item(i), "");
        System.out.println("############################");
    }//from  w  ww  .j  av  a  2  s. co m
}

From source file:Main.java

public static void removeElementXML(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {// www  .  j  a va  2 s.c  o m
        // Visit the children
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeElementXML(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static Collection<Element> getChildElements(Node node) {
    List<Element> elements = new ArrayList<Element>();
    NodeList nodes = node.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node childNode = nodes.item(i);
        if (childNode instanceof Element) {
            elements.add((Element) childNode);
        }/*from  w ww  . java  2s  .c o  m*/
    }
    return elements;
}

From source file:Main.java

/**
 * Return array of children nodes/*from w  w  w .  j  a v a  2s .  c o  m*/
 */
public static Node[] childrenArray(Node node) {
    NodeList list = node.getChildNodes();
    Node[] children = new Node[list.getLength()];
    for (int i = 0; i < children.length; i++) {
        children[i] = list.item(i);
    }
    return children;
}

From source file:Main.java

public static Vector getChildrenElementsByTag(Node node, String name) {
    Vector result = new Vector();
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
        if (nl.item(i) instanceof Element
                && (((Element) nl.item(i)).getTagName().equals(name) || name == null)) {
            result.add(nl.item(i));/* w ww . j a  v  a2s. c  o m*/
            //System.out.println(nl.item(i).toString());
            //System.out.println(((Element)nl.item(i)).getAttribute("className"));
            //System.out.println(((Element)nl.item(i)).getTagName());
        }
    return result;
}

From source file:Main.java

/**
 * Returns the value of an attribute in the first element in a document with a given tag name.
 * This is useful for well structured documents when it is known that there is only
 * one such element and that it is has that attribute.
 * //from w w  w . ja  va 2 s  .c o  m
 * @param document The document to search within.
 * @param tagname The name of the element to access.
 * @param attributename The attribute's name.
 * @return The value of the attribute of the first respective element, or the empty string, if
 * the element of the attribute could not be found.
 */
public static String getAttribute(Document document, String tagname, String attributename) {
    NodeList list = document.getElementsByTagName(tagname);
    if (list.getLength() < 1) {
        return "";
    }
    Element tag = (Element) list.item(0);
    return tag.getAttribute(attributename);
}

From source file:Main.java

public static int getSize(Element el, String tagName) {
    NodeList list = el.getElementsByTagName(tagName);
    return list.getLength();
}

From source file:Main.java

public static Collection<Node> search_nodes_by_name(Node root, String name) {
    Collection<Node> result = new LinkedList<Node>();
    if (root.getNodeName().equals(name))
        result.add(root);//  ww  w .j a  v  a  2 s .  c  om
    NodeList list = root.getChildNodes();
    for (int i = 0; i < list.getLength(); ++i) {
        Node child = list.item(i);
        Collection<Node> ret = search_nodes_by_name(child, name);
        result.addAll(ret);
    }
    return result;
}

From source file:Main.java

/**
 * Recursively removes all text nodes containing whitespace only from a document element. Also
 * trims leading and trailing whitespace from text nodes.
 * //  w  w  w  .j  ava  2  s. co  m
 * @param e The root document element.
 */
public static void removeWhitespaceNodes(Element e) {
    NodeList children = e.getChildNodes();
    for (int i = children.getLength() - 1; i >= 0; i--) {
        Node child = children.item(i);
        if (child instanceof Text && ((Text) child).getData().trim().length() == 0)
            e.removeChild(child);
        else if (child instanceof Text)
            child.setTextContent(((Text) child).getData().trim());
        else if (child instanceof Element)
            removeWhitespaceNodes((Element) child);
    }
}