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 void removeAll(Node node, short nodeType, String name) {
    if (node.getNodeType() == nodeType && (name == null || node.getNodeName().equals(name))) {
        node.getParentNode().removeChild(node);
    } else {//from ww w .  j  av  a  2s .c om
        NodeList list = node.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            removeAll(list.item(i), nodeType, name);
        }
    }
}

From source file:Main.java

public static Element getElement(Document doc, String tagName, int index) {
    // given an XML document and a tag
    // return an Element at a given index
    NodeList rows = doc.getDocumentElement().getElementsByTagName(tagName);
    return (Element) rows.item(index);
}

From source file:Main.java

/**
 * Helper class to help parse the server response.
 * /*w ww.  j  a  v a 2s.c o m*/
 * @param nodeList
 * @return
 */
public static String getNodeTrimValue(NodeList nodeList) {
    Element element = (Element) nodeList.item(0);
    String nodeValue = "";

    if (element != null) {
        NodeList itemNodeList = element.getChildNodes();

        int length = itemNodeList.getLength();

        for (int i = 0; i < length; i++) {
            Node node = ((Node) itemNodeList.item(i));
            if (node != null)
                nodeValue += node.getNodeValue();
        }

        if (nodeValue != null && !nodeValue.equals("")) {
            return nodeValue.trim();
        } else {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * get the first element in the parent element that's named name or null if no such element exists
 *//*from w  ww .  ja  v a  2  s. c o  m*/
public static Element getFirstElement(Element parent, String name) {
    NodeList list = parent.getElementsByTagName(name);

    Node node = list.item(0);
    if (node == null) {
        //throw new RuntimeException("Malformed XML: No such node: " + name);
        return null;
    }

    if (!isElement(node)) {
        throw new RuntimeException("XML Parsing error: " + name + " is not an Element");
    }

    return (Element) node;
}

From source file:Main.java

public static void printNodeList(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println(nodes.item(i).getNodeValue());
    }/*  w  w  w  .jav a  2s. c  om*/
}

From source file:Main.java

/**
 * Gets tag value from XML entry./*from w  ww.j  a va 2s.  co  m*/
 * @param tag         XML tag String.
 * @param xmlElement   XML Element Object.
 * @return            Integer.parseInt(((Node) lastNodeList.item(0)).getNodeValue()).
 */
public static String getEntry(String tag, Element xmlElement) {
    NodeList nodeList = xmlElement.getElementsByTagName(tag);
    Element element = (Element) nodeList.item(0);
    NodeList lastNodeList = element.getChildNodes();
    return lastNodeList.item(0).getNodeValue();
}

From source file:Main.java

/**Returns the first ELEMENT with name str, while ignoring whitespace nodes
 * //from   w w  w.  j ava2s  .  co  m
 * @param elm 
 * @param str
 * @return the String value of a child node of Element elm that has a name str
 * or null if no match
 */
public static Element getChildElementByName(Element elm, String str) {
    NodeList nList = elm.getElementsByTagName(str);
    return nList == null ? null : (Element) nList.item(0);
}

From source file:Main.java

public static Node getRootNode(Document doc) {
    NodeList l = doc.getChildNodes();
    if (l != null && l.getLength() == 1)
        return l.item(0);

    return null;/* w w w  .j a v  a2 s.  c o  m*/
}

From source file:Main.java

public static Object getBean() {
    try {//from w  w w  .  j a  va  2  s.  c o m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File("src/config.xml"));

        NodeList nl = doc.getElementsByTagName("className");
        Node node = nl.item(0).getFirstChild();
        String cName = node.getNodeValue();

        Class clazz = Class.forName(cName);
        Object obj = clazz.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static String getCharttype() {
    try {/*from   w  w  w  .j a va 2  s.  c o  m*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(configPath));

        NodeList nl = doc.getElementsByTagName("chartType");
        Node classNode = nl.item(0).getFirstChild();
        String chartType = classNode.getNodeValue().trim();
        return chartType;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}