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

/**
 * Returns the child of the given node which has the specified name
 * If no such node exists, returns null/*  ww w . j av  a  2s . co  m*/
 */
public static Node findChild(Node parent, String name) {
    // Iterate through the collection of children
    NodeList nodes = parent.getChildNodes();
    for (int a = 0; a != nodes.getLength(); ++a) {
        Node node = nodes.item(a);
        if (node.getNodeName().equals(name)) {
            // This is the one
            return node;
        }
    }

    // No such node
    return null;
}

From source file:Main.java

public static Node getFirstValidNode(Node node) {
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (isValidNode(children.item(i)))
            return children.item(i);
    }/*from  ww w . ja  v  a 2 s  . c  om*/

    return null;
}

From source file:Main.java

/**
 * get all child elements matching the given tag 
 * //  w  w  w .ja  v  a2s .  c  om
 * @param tag
 * @param searchIn
 * @return
 */
public static ArrayList<Element> getChildElementsByTag(String tag, Element searchIn) {
    ArrayList<Element> toReturn = new ArrayList<Element>();
    NodeList list = searchIn.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        if (n instanceof Element && ((Element) n).getTagName().equals(tag)) {
            toReturn.add((Element) n);
        }

    }
    return toReturn;
}

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   ww w. ja  v  a 2 s .  com

    return elements;
}

From source file:Main.java

/**
 * Removes all Whitespace Noted from the specified element.
 * @param e Element// ww  w.  j ava 2 s .  c  o m
 */
private 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 Element) {
            removeWhitespaceNodes((Element) child);
        }
    }
}

From source file:Main.java

/**Get a NodeLIst of child nodes, ignoring nodes that have White space
 * @param e/*from  w  w  w .j a v a 2  s . c  o m*/
 * @return
 */
public static NodeList getChildNodesNoWS(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);
        }

    }
    return e.getChildNodes();
}

From source file:Main.java

public static Node findChildNode(Node node, String name) {
    Node child;// w  w  w . j  ava2s.com
    NodeList list;

    list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        child = list.item(i);
        if (child instanceof Element && child.getNodeName().equals(name)) {
            return child;
        }
    }

    return null;
}

From source file:Main.java

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());
        }/*w w  w .  jav a  2  s.co m*/
    }
    return value.toString().trim();
}

From source file:Main.java

public static List<? extends Node> get_childs(Node parent, String child_name) {
    List<Node> result = new LinkedList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = children.item(i);
        if (child.getNodeName().equals(child_name))
            result.add(child);//from   ww w  . java2s.c o m
    }
    return result;
}

From source file:Main.java

public static void getInnerText(StringBuilder sb, Element elt, String separator) {
    NodeList tns = elt.getChildNodes();
    for (int j = 0; j < tns.getLength(); j++) {
        Node tn = tns.item(j);
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        } else if (tn.getNodeType() == Node.ELEMENT_NODE) {
            sb.append(separator);/* w w w .  java2 s. c om*/
            getInnerText(sb, (Element) tn, separator);
            sb.append(separator);
        }
    }
}