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

/**
 * Preskoci uvodni komentare apod.//  w w w .  j  ava 2 s  .co m
 * 
 * @param node
 * @return prvni subelement (jediny - korenovy - element v dokumentu)
 */
public static Element getFirstElement(Node node) {
    NodeList childNodes = node.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node childNode = childNodes.item(i);
        if (childNode instanceof Element) {
            return (Element) childNode;
        }
    }
    return null;
}

From source file:Main.java

public static ArrayList<String> getArrayListTextValuesByDocument(Document document, String tag) {
    ArrayList<String> result = new ArrayList<String>();

    NodeList nodeList = document.getElementsByTagName(tag);
    int length = nodeList.getLength();
    for (int i = 0; i < length; i++)
        result.add(nodeList.item(i).getTextContent());

    return result;
}

From source file:Main.java

public static Map<String, String> getChildElements(Node parent) {
    Map<String, String> elements = new HashMap<String, String>();

    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        elements.put(child.getNodeName(), child.getTextContent());
    }//from www  .j a  va2s. c  o  m

    return elements;
}

From source file:Main.java

/**
 * Delete children elements for Element by element name
 *//*from w  w  w .ja  va  2  s .co m*/
public static void removeAllChildren(Element node, String elementName) {
    if (node != null) {
        NodeList nl = node.getChildNodes();
        int len = nl.getLength();
        for (int i = 0; i < len; i++) {
            Node childNode = nl.item(i);
            if (childNode != null && childNode.getLocalName() != null
                    && childNode.getLocalName().equals(elementName))
                node.removeChild(childNode);
        }
    }
}

From source file:Main.java

public static Element[] getChildrenByAttrubte(Element parent, String attrName, String attrValue) {
    NodeList nodes = parent.getChildNodes();
    int len = nodes.getLength();
    Element temp;/*ww  w .j  a  v a 2 s .co m*/
    List<Element> list = new ArrayList<Element>(len);
    if (nodes != null && len > 0) {
        for (int i = 0; i < len; i++) {
            if (nodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
                temp = (Element) nodes.item(i);
                if (getAttribute(temp, attrName).equalsIgnoreCase(attrValue))
                    list.add(temp);
            }
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

public static String[] getStringValuesForXMLTag(Element xmlElement, String key) {
    NodeList nl = xmlElement.getElementsByTagName(key);

    if (nl.getLength() > 0) {

        String[] output = new String[nl.getLength()];

        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i).getFirstChild();
            if (node != null) {
                output[i] = node.getNodeValue().trim();
            }//w ww .  jav a 2 s  .co  m
        }
        return output;
    }
    return null;
}

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;
    }/*  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.
 * // ww  w  .j  a  v  a 2  s. c  o m
 * @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

/**
 * Extract the text in an element./*from www  .  ja va  2 s  .  c o  m*/
 * Concats data of Text and CDATASection elements.
 */
public static String getText(Element elem) {

    StringBuffer text = new StringBuffer();
    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Text)
            text.append(((Text) n).getData());
        else if (n instanceof CDATASection)
            text.append(((CDATASection) n).getData());
    }
    return text.toString();
}

From source file:Main.java

public static Node[] getChildNodesByName(Element parent, String name) {
    List<Node> nodeList = new ArrayList<Node>();

    NodeList childNodes = parent.getChildNodes();
    int length = childNodes.getLength();
    for (int i = 0; i < length; i++) {
        Node current = childNodes.item(i);
        if (current.getNodeName().equals(name))
            nodeList.add(current);/*w ww.  j  a  v  a2s  .  co  m*/
    }

    Node[] nodes = new Node[nodeList.size()];
    nodeList.toArray(nodes);
    return nodes;
}