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 String getTagValue(String tag, Element elem) {
    String retVal = "";
    NodeList fstNmElmntLst = elem.getElementsByTagName(tag);
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    if (fstNmElmnt == null)
        return "";
    NodeList fstNm = fstNmElmnt.getChildNodes();
    if (fstNm == null || fstNm.item(0) == null)
        return "";
    retVal = fstNm.item(0).getNodeValue();
    return retVal;
}

From source file:Main.java

public static String GetFirstInstance(NodeList nList) {
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null)
                return eElement.getTextContent();
        } // end if nnode.getnodetype()
    } //end for int temp
    return new String("");
}

From source file:Main.java

public static void removeChildren(Node e) {
    NodeList list = e.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node n = list.item(i);
        e.removeChild(n);/*from  w  ww . ja v  a 2  s . c  om*/
    }
}

From source file:Main.java

public static String getTagValue(String sTag, Element eElement) {
    //      NodeList nlList = eElement.getElementsByTagName(sTag).item(0)
    //            .getChildNodes();
    String value = null;/* w  w w  . jav a 2s . c o  m*/
    NodeList nlList = eElement.getElementsByTagName(sTag);
    if (nlList != null && nlList.item(0) != null) {
        value = ((Node) nlList.item(0).getChildNodes().item(0)).getNodeValue();
    }
    return value;
}

From source file:Main.java

public static void removeNodeListContent(NodeList nodeList) {
    while (nodeList.getLength() > 0) {
        removeNode(nodeList.item(0));
    }//from w w  w.  j  a v  a  2 s.  c o m
}

From source file:Main.java

public static Object getBean(String relativePath, String nodeName) {
    try {//from w w  w  .  j av a  2  s .  c  o  m
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File(path + relativePath));

        NodeList nl = doc.getElementsByTagName(nodeName);
        Node classNode = nl.item(0).getFirstChild();
        String className = classNode.getNodeValue().trim();

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

From source file:Main.java

public static Element getRoot(Document document, String root) {
    if (document == null) {
        throw new IllegalArgumentException("Null document");
    }/*from  www .ja va 2  s  .  co m*/

    NodeList nodeList = document.getElementsByTagName(root);
    return nodeList.item(0) != null ? (Element) nodeList.item(0) : null;
}

From source file:Main.java

public static Object getBean(String filePath) {
    try {/* w w  w  .  j  a v a 2s .  c  om*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document doc;
        doc = builder.parse(new File(filePath));

        NodeList nl = doc.getElementsByTagName("className");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();
        System.out.println(cName);
        Class c = Class.forName(cName);
        Object obj = c.newInstance();
        return obj;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Returns the first node in the node list that is an element.
 * //ww w .ja  v a 2s  .co  m
 * @param nodes The list of nodes that contains the element to be
 *              determined.
 * 
 * @return The first element in the given node list.
 */
public static Node getFirstElement(NodeList nodes) {
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            return node;
        }
    }
    return null;
}

From source file:Main.java

public static boolean hasChild(Node xml, String name) {
    NodeList nl = xml.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);
        if (name.equalsIgnoreCase(n.getNodeName()))
            return true;
    }// w w w . j  av  a2s.  c  om
    return false;
}