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

/**
 * Returns the text value of the specified node. The returned value is the node value of the first child of <code>node</code> which type
 * is <code>Document.TEXT_NODE</code>.
 * //from  w w  w.j  a  v a  2s.co m
 * @param node
 *            the node which text value has to be retrieved.
 * @return the text value of the node.
 */
public static String getTextValue(Node node) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).getNodeType() == Document.TEXT_NODE) {
            return list.item(i).getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the children element of an XML node
 *//* w  w  w .j  ava 2  s .  co m*/
public static Iterable<Element> children(Node n) {
    final List<Element> e = new ArrayList<Element>();
    final NodeList ns = n.getChildNodes();
    for (int i = 0; i < ns.getLength(); i++) {
        if (ns.item(i) instanceof Element) {
            e.add((Element) ns.item(i));
        }
    }
    return e;
}

From source file:Main.java

License:asdf

public static void newEmail(Document doc, String newname, String newemail) {
    Element root = doc.getDocumentElement();
    NodeList rootlist = root.getChildNodes();
    for (int i = 0; i < rootlist.getLength(); i++) {
        Element person = (Element) rootlist.item(i);
        NodeList personlist = person.getChildNodes();
        Element name = (Element) personlist.item(0);
        NodeList namelist = name.getChildNodes();
        Text nametext = (Text) namelist.item(0);
        String oldname = nametext.getData();
        if (oldname.equals(newname)) {
            Element email = (Element) personlist.item(1);
            NodeList emaillist = email.getChildNodes();
            Text emailtext = (Text) emaillist.item(0);
            emailtext.setData(newemail);
        }/*from   w w  w  . j  a v a2s.  c o  m*/
    }
}

From source file:Main.java

public static Element getChildElementByTagName(Element ele, String childEleName) {
    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);/*ww  w .  ja va  2s . c o  m*/
        if (node instanceof Element && nodeNameMatch(node, childEleName)) {
            return (Element) node;
        }
    }
    return null;
}

From source file:Main.java

/** Returns the first child element with the given name from an element.
 *
 *  @param node The parent element to be searched
 *  @param name The element name to search for
 *  @return Node representing the element
 *///from  www  . j a v a2s .c o  m
public static Node getSubnodeByName(Node node, String name) {
    Node retVal = null;
    NodeList nl = node.getChildNodes();
    if (nl != null && nl.getLength() != 0) {
        for (int i = 0; i < nl.getLength(); i++)
            if (nl.item(i).getNodeName().equals(name)) {
                retVal = nl.item(i);
                break;
            }
    }
    return retVal;
}

From source file:Main.java

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

From source file:Main.java

public static void removeChilds(Node node) {
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
        node.removeChild(nl.item(i));/*from   w w  w .  ja v  a2s  .c o m*/
}

From source file:Main.java

private static Node findNode(Node node, String fieldName) {
    NodeList Nodes = node.getChildNodes();
    for (int i = 0; i < Nodes.getLength(); i++) {
        Node fieldNode = Nodes.item(i);
        String name = fieldNode.getNodeName();
        if (name != null && name == fieldName) {
            return fieldNode;
        }/*w w w  .jav  a 2  s  . c  o m*/
    }
    return null;
}

From source file:Main.java

public static String getText(Node node) {
    String value = "";

    NodeList children = node.getChildNodes();
    for (int k = 0; k < children.getLength(); k++) {
        Node child = children.item(k);
        if (child.getNodeType() == Node.TEXT_NODE) {
            value = child.getNodeValue();
        }/* w ww  . j ava  2s . c o  m*/
    }

    return value;
}

From source file:Main.java

/**
 * Returns the first child node matching the {@code tagName}.
 *///from   w w  w. ja v  a2s .  c  o  m
public static Node findNode(Node parent, String tagName) {
    Node result = null;

    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeName().equals(tagName)) {
            return nl.item(i);
        }
    }

    return result;
}