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 Element getChildElement(Element parent, String childName, String childNamespace) {
    NodeList ns = parent.getChildNodes();
    for (int i = 0; i < ns.getLength(); i++) {
        Node n = ns.item(i);
        if (n instanceof Element) {
            Element child = (Element) n;
            if (childName.equals(child.getLocalName()) && childNamespace.equals(child.getNamespaceURI())) {
                return child;
            }/*  www .ja  v a2s  .co m*/
        }
    }
    return null;
}

From source file:Main.java

public static String getTagValue(String sTag, Element eElement) {
    String ret = null;//from  ww  w .ja  v  a2 s . c o m

    try {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
        Node nValue = (Node) nlList.item(0);
        ret = nValue.getNodeValue();
    } catch (NullPointerException e) {
        // System.t.println("Null pour l'item : " + sTag);
    }
    return ret;
}

From source file:Main.java

static public String getNodeValue(String tagName, NodeList nodes) {
    for (int x = 0; x < nodes.getLength(); x++) {
        Node node = nodes.item(x);
        if (node.getNodeName().equalsIgnoreCase(tagName)) {
            NodeList childNodes = node.getChildNodes();
            for (int y = 0; y < childNodes.getLength(); y++) {
                Node data = childNodes.item(y);
                if (data.getNodeType() == Node.TEXT_NODE) {
                    return data.getNodeValue();
                }//w  ww  .  jav  a 2s . c om
            }
        }
    }
    return "";
}

From source file:Main.java

/** Returns the first element with the given name from a document.
 * //  w w w.jav a2  s . c  om
 *  @param doc Document to be searched
 *  @param name The element name to search for
 *  @return Node representing the element
 */
public static Node getNodeByName(Document doc, String name) {
    Node retVal = null;
    NodeList nl = doc.getElementsByTagName(name);
    if (nl != null && nl.getLength() != 0)
        retVal = nl.item(0);
    return retVal;
}

From source file:Main.java

public static Object getBean() {
    try {/*w  ww.ja va 2  s .  c o  m*/
        DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document document;
        document = builder.parse(new File("config.xml"));

        NodeList nl = document.getElementsByTagName("StrategyClassName");
        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();

        System.out.println(cName);
        Class c = Class.forName("com.seven.strategy.sort." + cName);
        Object object = c.newInstance();
        return object;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static Node getSingleNodeElementByTagName(String tagName) throws Exception {
    NodeList list = getNodeList(tagName);
    Node node = null;//from  w ww  . j a  va  2s.c  om
    if (list.getLength() > 0 && list.item(0).getNodeType() == Node.ELEMENT_NODE) {
        node = list.item(0);
    } else {
        throw new Exception("Xpath Query did not result in a Node element. Check your Xpath expression");
    }
    return node;
}

From source file:Main.java

/**
 * Gets node index/*w  w  w  .  j a  v a2  s  . c om*/
 * @param parentNode note in which the search
 * @param node seeking node
 * @return node index. -1 if node does not exists
 */
public static int getNodeIndex(Node parentNode, Node node) {
    NodeList list = parentNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).equals(node)) {
            return i;
        }
    }
    return -1;
}

From source file:Utils.java

/**
 * Return the first named Element found.  Null if none.
 * @param element the containing Element
 * @param name the tag name/* www  . j  av a2s .c om*/
 * @return matching Element (null if none)
 */
public static Element getElement(Element element, String name) {
    NodeList nodeList = getElementList(element, name);
    return (nodeList.getLength() == 0) ? null : (Element) nodeList.item(0);
}

From source file:Main.java

public static Node getFirstElementByTagName(Document doc, String tagName) {
    NodeList list = doc.getElementsByTagName(tagName);
    if (list.getLength() > 0) {
        return list.item(0);
    } else {//from  ww w. ja  va  2s  .c o  m
        return null;
    }
}

From source file:Main.java

/**
 * Get the body value of a node/*from   w w  w  .  ja  v  a  2s .c  o m*/
 */
public static String getNodeBodyValue(Node child) {
    String bodyValue = "";
    NodeList bodies = child.getChildNodes();
    for (int j = 0; j < bodies.getLength(); j++) {
        bodyValue += bodies.item(j).getNodeValue();
    }
    return bodyValue;
}