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

protected static Node getNodeFromNodeList(NodeList list, String name) {

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeName().equals(name)) {
            return node;
        }//from w ww  .j a v a  2  s. co m
    }
    return null;
}

From source file:Main.java

/**
 * Returns the content of the first CDATA section node found under the specified node.
 * /*from   ww w  . java  2  s  .  c o  m*/
 * @param node
 *            the node.
 * @return the content of the first CDATA section node.
 */
public static String getCDataSectionContent(Node node) {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        if (list.item(i).getNodeType() == Document.CDATA_SECTION_NODE) {
            return list.item(i).getNodeValue();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first child node matching the {@code tagName}.
 *///w  w w. j  a  v a  2s .c o  m
public static Node findNode(Node parent, String tagName) {
    Node result = null;

    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeName().equals(tagName)) {
            return nl.item(i);
        }
    }

    return result;
}

From source file:Main.java

public static Node getChildByName(Node node, String name) {
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeName().equals(name))
            return nl.item(i);
    }//from  www  .j  a  v a2 s .  c  o  m
    return null;
}

From source file:Main.java

public static Node getChildNode(Node parentNode, String childElementName) {
    NodeList l = parentNode.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node node = l.item(i);
        if (node.getNodeName().equals(childElementName))
            return node;
    }//from w ww  .  j  av a  2s.  c om
    return null;
}

From source file:Main.java

/**
 * This method return role name from xml file.
 * @param element String- privilege Element
 * @param elementName -Element name for which value has to be return
 * @return String Role name/*w  w w  .j  a  v a 2s  . co m*/
 */
public static String getElementValue(Element element, String elementName) {
    String roleName = "";
    NodeList elementList = element.getElementsByTagName(elementName);
    Element ele = (Element) elementList.item(0);
    NodeList valueNodeList = ele.getChildNodes();
    Node node = ((Node) valueNodeList.item(0));
    if (node != null) {
        roleName = node.getNodeValue();
    }
    return roleName;
}

From source file:Main.java

/**
 * Get the value of the specified element
 *
 * @param element/*from  w w  w .j a v  a2  s  . c o  m*/
 * @return
 */
public static String getElementValue(Element element) {
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i).getNodeType() == Node.TEXT_NODE) {
            return element.getFirstChild().getNodeValue();
        }
    }

    return null;
}

From source file:Main.java

public static String findNodeValue(Element firstElement, String name) {
    String nodeValue = null;// w  ww.  j  av a 2  s.  co  m
    NodeList firstNameList = firstElement.getElementsByTagName(name);
    Element firstNameElement = (Element) firstNameList.item(0);

    NodeList textFNList = firstNameElement.getChildNodes();

    if (textFNList.getLength() > 0) {
        nodeValue = (textFNList.item(0)).getNodeValue();
    }
    return nodeValue;
}

From source file:Utils.java

/**
 * /*from   w w w  . ja  va2s .c o m*/
 */
public static void moveContent(Element from, Element to) {
    // lets move the child nodes across
    NodeList childNodes = from.getChildNodes();
    while (childNodes.getLength() > 0) {
        Node node = childNodes.item(0);
        from.removeChild(node);
        to.appendChild(node);
    }
}

From source file:Main.java

private static void printNote(NodeList nodeList, int depth) {
    for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
            System.out.println(depth + "Node Value =" + tempNode.getTextContent());
            if (tempNode.hasAttributes()) {
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                }// www .  j a  v a 2s .c  o  m
            }
            if (tempNode.hasChildNodes()) {
                printNote(tempNode.getChildNodes(), depth + 1);
            }
            System.out.println(depth + "Node Name =" + tempNode.getNodeName());
        }
    }
}