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

public static Node getTag(Element root, String tagName) {
    NodeList list = root.getElementsByTagName(tagName);
    for (int loop = 0; loop < list.getLength(); loop++) {
        Node node = list.item(loop);
        System.out.println("nodeName : " + node.getNodeName() + ":::::" + node.getNodeType());
        if (node.getNodeName().equals(tagName)) {
            return node;
        }/*from   w  ww  .  ja  va2  s  . com*/
    }
    return null;
}

From source file:Main.java

public static Node findChildWithTagName(Node parent, String tagName) {
    if (parent == null) {
        return null;
    }//from  w  ww. ja  va2 s. c o m

    NodeList childs = parent.getChildNodes();
    for (int i = 0; i < childs.getLength(); i++) {
        Node child = childs.item(i);
        if (child.getNodeName().equals(tagName)) {
            return child;
        } else if (child.hasChildNodes()) {
            Node result = findChildWithTagName(child, tagName);
            if (result != null) {
                return result;
            }
        }
    }

    return null;
}

From source file:Main.java

/**
 * Indicates whether element has any child element.
 *
 * @param element the namespace to analyze.
 * @return true if element has any child element otherwise false.
 *///from w w w . j  a  va2s . co m
public static boolean hasChildElements(Element element) {
    NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }
    return false;
}

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.
 *///from   ww  w .j a v a2 s  .co  m
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());
    }/*from  w ww .j  ava2  s. co  m*/
    return ret.toString();
}

From source file:Main.java

public static String getText(Element e) {
    NodeList nl = e.getChildNodes();
    int max = nl.getLength();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            return n.getNodeValue();
        }/*from  www  .  jav  a2s  . c o  m*/
    }
    return "";
}

From source file:Main.java

public static List<Element> getChildElementsByName(Element root, String tagName) {
    List<Element> tags = new LinkedList<Element>();
    NodeList nl = root.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node child = nl.item(i);
        if (child.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }//w w w . jav a2 s . c  o  m
        if (tagName.equals(child.getNodeName())) {
            tags.add((Element) child);
        }
    }
    return tags;
}

From source file:Main.java

public static Collection<Node> getNodesByName(Node node, String name) {
    Collection<Node> list = new LinkedList<Node>();

    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++)
        if (nl.item(i).getNodeName().equals(name))
            list.add(nl.item(i));//from  ww  w.  j a  v a  2 s. co m

    return list;
}

From source file:Main.java

public static Element getSingleOptionalChildElement(Element parent, Enum<?> child) {
    final String elName = getXmlName(child);
    final NodeList list = parent.getElementsByTagName(elName);
    if (list.getLength() == 1) {
        return (Element) list.item(0);
    }//from www .ja  va  2 s  .  c om
    if (list.getLength() == 0) {
        return null;
    }
    throw new IllegalArgumentException("expected a single " + elName + " child element of XML element "
            + parent.getNodeName() + ", but found " + list.getLength());
}

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 ww.  j  av a2  s.c o m*/
    }
}