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

static String[] getNodeValue(NodeList nodes) {
    String checkIds[] = new String[nodes.getLength()];
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        checkIds[i] = node.getTextContent();
    }//from  w ww .  j  a  v a 2  s . com
    return checkIds;
}

From source file:Main.java

/**
 * Gets the first child element./*w  w  w . jav a  2s. c om*/
 * 
 * @param e the element.
 * @return the first child element.
 */
public static Element getFirstChildElement(Element e) {
    if (e != null) {
        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) node;
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * This method return the first named node with a certain name from a node list
 * //from   w  w w .jav a 2  s. com
 * @param list - Investigated list
 * @param name - The wanted name
 * @return The first node with a certain name from a node list
 */
public static Element getFirstNamedNode(NodeList list, String name) {
    Element result = null;

    for (int i = 0; i < list.getLength(); i++)
        if (list.item(i).getNodeName().equals(name)) {
            result = (Element) list.item(i);
            break;
        }

    return result;
}

From source file:Main.java

public static String getAttribute(String xmlStr, String tagName, String attrName) {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(false); // never forget this!
    Document doc = null;/*w  w w.  j a  v  a2  s. c o  m*/
    DocumentBuilder builder = null;
    String value = null;

    try {
        builder = domFactory.newDocumentBuilder();
        doc = builder.parse(new ByteArrayInputStream(xmlStr.getBytes()));
        // Get the root element
        Node rootNode = doc.getFirstChild();
        NodeList nodeList = doc.getElementsByTagName(tagName);
        if ((nodeList.getLength() == 0) || (nodeList.item(0).getAttributes().getNamedItem(attrName) == null)) {
            logger.error("Either node " + tagName + " or attribute " + attrName + " not found.");
        } else {
            value = nodeList.item(0).getAttributes().getNamedItem(attrName).getNodeValue();
            logger.debug("value of " + tagName + " attribute: " + attrName + " = " + value);
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }

    return value;
}

From source file:Main.java

public static Element selectSingleElement(Element parent, String elementName) {
    NodeList list = parent.getElementsByTagName(elementName);

    if (list.getLength() > 0) {
        return (Element) list.item(0);
    }/*from w  ww. ja  v a  2s. c o  m*/

    return null;
}

From source file:Main.java

public static String getSimpleElementText(Element node) {
    if (node == null)
        return null;
    StringBuffer sb = new StringBuffer();
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (child instanceof Text) {
            sb.append(child.getNodeValue());
        }// ww w  .ja  v a2  s. c o  m
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Trys to find a child element in the given parent element where the child's
 * name attribute is the given value./*from  ww w.  j a v a 2s  .  c o  m*/
 *
 * @param parent The Element to search in.
 * @param name   The name attribute of the element to search for.
 *
 * @return The Element if found, null otherwise.
 */
public static Element findElementWithNameAttribute(Element parent, String name) {
    NodeList l = parent.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n.getNodeType() == n.ELEMENT_NODE) {
            Element e = (Element) n;
            if (e.getAttribute("name").equals(name)) {
                return e;
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getChildNodes(Element parent, String name) {
    List<Element> result = new ArrayList<Element>();
    NodeList childNodes = parent.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node n = childNodes.item(i);
        if (name.equals(n.getNodeName())) {
            result.add((Element) n);
        }/*from w  w w  .  j a  v  a 2s.  co  m*/
    }
    return result;
}

From source file:Main.java

public static List<Element> selectElementsByName(Element parent, String elementName) {
    ArrayList<Element> result = new ArrayList<Element>();

    NodeList list = parent.getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);

        if ((node instanceof Element) && node.getNodeName().equals(elementName)) {
            result.add((Element) node);
        }// w w  w  .j  a  va  2  s. co m
    }

    return result;
}

From source file:Main.java

/**
 * Gets the first node with the specified tag name from the given document.
 * If there are multiple nodes with the specified tag name, the first node
 * found will be returned./*from w w w  .jav  a2  s  .  c  o m*/
 * 
 * @param doc
 *            the document
 * @param tagname
 *            the tag name of the node to find.
 * @return the first node found with the specified tag name or
 *         <code>null</code> if no node with that name could be found.
 */
public static Node getNode(Document doc, String tagname) {
    NodeList nl = doc.getElementsByTagName(tagname);
    if (!isEmpty(nl)) {
        return nl.item(0);
    }
    return null;
}