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

/**
 * Return the contained text within an Element. Returns null if no text found.
 *///from  w w  w  .j a v a 2 s.  c o  m
public final static String getElementText(Element element) {
    NodeList nl = element.getChildNodes();

    for (int i = 0; i < nl.getLength(); i++) {
        Node c = nl.item(i);

        if (c instanceof Text) {
            return ((Text) c).getData();
        }
    }

    return null;
}

From source file:Main.java

public static Object getBean() {
    try {//from ww  w.  j a va2  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("TemplateClassName");

        Node classNode = nl.item(0).getFirstChild();
        String cName = classNode.getNodeValue();

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

From source file:Main.java

public static Element getChildElementByTagName(Element ele, String childEleName) {
    NodeList nl = ele.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node instanceof Element && nodeNameMatch(node, childEleName)) {
            return (Element) node;
        }/*from w w  w  .j  ava 2 s. c o m*/
    }
    return null;
}

From source file:Main.java

private static Node getSingleNodeElementByTagName(String tagName, int indexZeroBased) throws Exception {
    NodeList list = getNodeList(tagName);
    Node node = null;/*from  w  ww. java2  s.c om*/

    if (list.getLength() > 0 && list.item(indexZeroBased).getNodeType() == Node.ELEMENT_NODE) {
        node = list.item(indexZeroBased);
    } else {
        throw new Exception("Xpath Query did not result in any Node elements. Check your Xpath expression");
    }

    return node;
}

From source file:Main.java

public static List<Element> getChildElements(Element elt, String name) {
    List<Element> list = new ArrayList<Element>();

    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(child.getNodeName()))
                list.add((Element) child);
    }/* ww w.j a  va2  s. c o  m*/
    return list;
}

From source file:Main.java

public static Element getFirstNode(Element elem, String name) {
    NodeList nl = elem.getElementsByTagName(name);
    if (nl.getLength() > 0)
        return (Element) nl.item(0);
    return null;/*w  ww  . j a v a  2  s  .com*/
}

From source file:Main.java

/** Return first child with given name within given element. */
public static Element getFirst(Element parent, String name) {
    NodeList nl = parent.getChildNodes();
    for (int i = 0, l = nl.getLength(); i < l; i++) {
        Node curr = nl.item(i);
        if (curr.getNodeType() == Element.ELEMENT_NODE && curr.getNodeName().equals(name))
            return (Element) curr;
    }/*from   w w w  .j  av  a2  s  .  c o  m*/

    return null;
}

From source file:Main.java

/**
 * Get a child element with a specific name.
 *
 * @param name Name of the requested child element.
 * @param elem Parent element.//from   w w  w  .j  ava 2  s.c om
 *
 * @return The first child element with the given name, or null if none found.
 */
public static Element getChildElement(String name, Element elem) {

    NodeList l = elem.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);
        if (n instanceof Element && n.getNodeName().equals(name))
            return (Element) n;
    }
    return null;
}

From source file:Main.java

public static final String getText(Node node) {
    if (node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0) {
            Node child = childNodes.item(0);
            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                return child.getNodeValue();
            }//from w  ww  . j a v  a2s . co  m
        }
    }
    return null;
}

From source file:Main.java

/**
 * Checks if the given {@link NodeList} contains a child element.
 *
 * @param list The {@link NodeList} to check.
 * @return Whether the {@link NodeList} contains children.
 */// w ww .  j a  va2 s . c  o m
public static boolean hasChildElement(NodeList list) {

    Node n;

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

        if (n.getNodeType() == Node.ELEMENT_NODE) {
            return true;
        }
    }

    return false;

}