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

/**
 * Create a child of the given node at the given index.
 * // w  w  w  . j a  v  a  2s .c o m
 * @param doc
 *            a document
 * @param element
 *            an element
 * @param index
 *            an index
 * @param nodeName
 *            a node name
 * @return org.w3c.dom.Element
 */
public static Element createChildElement(Document doc, Element element, int index, String nodeName) {
    Element element2 = doc.createElement(nodeName);
    try {
        NodeList childList = element.getElementsByTagName(nodeName);
        Node child = childList.item(index);
        element.insertBefore(element2, child);
    } catch (Exception e) {
        element.appendChild(element2);
    }
    return element2;
}

From source file:Main.java

public static Node getFirstChildElementNode(Node node) {
    if (node == null)
        return (null);

    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        if (Node.ELEMENT_NODE == child.getNodeType())
            return (child);
    }/*from w ww . ja v a  2 s . c o  m*/

    return (null);
}

From source file:Main.java

public static Element readFirstChild(Node parentNode, String nodeName) {
    if (parentNode != null) {
        NodeList children = parentNode.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            if (children.item(i).getNodeName().equals(nodeName))
                return (Element) children.item(i);
        }/* www. j av a  2 s .  c om*/
    }
    return null;
}

From source file:Main.java

public static String getConfigParam(Document xml, String xPath) throws Exception {
    //        NodeList nodeList = XPathAPI.selectNodeList(xml, xPath);
    NodeList nodeList = selectNodeList(xml, xPath);
    if (nodeList.getLength() == 0)
        return null;
    return nodeList.item(0).getNodeValue();
}

From source file:Main.java

private static String getTextValue(Element ele, String tagName) {
    String textVal = null;/*from w  w  w . ja  v  a2 s  .c om*/
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:Main.java

public static Node getChildByName(Node parent, String childName) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node child = (Node) children.item(i);
        if (child.getNodeName().equals(childName)) {
            return child;
        }/*from   ww w.  ja v  a  2 s . co  m*/
    }
    return null;
}

From source file:Main.java

/**
 * Get child elements by classname//from  ww  w. j a  v  a  2s.co  m
 *
 * @param ele parent element
 * @param cname of elements to find
 */
public static List<Element> getElementsByClass(Element ele, String cname) {
    Vector<Element> list = new Vector();
    NodeList nl = ele.getChildNodes();
    for (int j = 0; j < nl.getLength(); j++) {
        if (nl.item(j).getNodeType() != Node.ELEMENT_NODE)
            continue;
        Element e = (Element) nl.item(j);
        if (e.getAttribute("class").equals(cname))
            list.add(e);
    }
    return (list);
}

From source file:Main.java

/**
 * A helper method for quick property saving/modification. If the property element does not exist, it is automatically created.
 *
 * @param element The parent element./*  www  . j a  v a  2s . c o m*/
 * @param name The property name.
 * @param value The property value
 * @return
 */
public static void quickPropertyWrite(Element element, String name, String value) {
    NodeList nodeList = element.getElementsByTagName(name);
    if (nodeList.getLength() == 1) {
        nodeList.item(0).setTextContent(value);
    } else {
        Element newProp = element.getOwnerDocument().createElement(name);
        newProp.setTextContent(value);
        element.appendChild(newProp);
    }
}

From source file:Main.java

/**
 * get single element by tag name//from   w  w  w  .j a v a  2 s. co  m
 * @param element
 * @param tag
 * @return
 */
public static List<Element> getChildElements(Element element) {
    List<Element> elems = new ArrayList<Element>();
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            elems.add(e);
        }
    }
    return elems;
}

From source file:Main.java

/**
 * Search a child node by type/*  ww w  .j  ava 2  s . com*/
 * 
 * @param parent   the parent node
 * @param nodeName   the node name
 * @param nodeType  the node type for searching
 * @return         Node with the specified name
 * @see            Node
 * @throws Exception
 */
public static Node findChildNodeByNameAndType(Node parent, String nodeName, String nodeType) throws Exception {
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (node.getNodeName().equals(nodeName)) {
            String strType = node.getAttributes().getNamedItem("type").getNodeValue();
            if (strType.equals(nodeType))
                return node;
        }
    }
    return null;
}