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 void removeElementFromXml(String xmlFile, String nodeName)
        throws ParserConfigurationException, SAXException, IOException, TransformerException {

    Document doc = getXmlDoc(xmlFile);

    NodeList nodes = doc.getChildNodes();

    NodeList childNodes = nodes.item(0).getChildNodes();

    for (int i = 0; i < childNodes.getLength(); i++) {

        if (childNodes.item(i).getNodeName().equals(nodeName)) {

            nodes.item(0).removeChild(childNodes.item(i));
        }/*w ww. j av  a2s  .  co m*/
    }
    saveXml(doc, xmlFile);
}

From source file:Main.java

private static String getNodeText(Element element, String nodeWeiZhi) {
    String result = "";
    String[] nodeNames = nodeWeiZhi.split(">");
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        String nodeName = node.getNodeName();
        if (nodeName.equals(nodeNames[0])) {
            if (nodeNames.length == 1) {
                result += "," + node.getTextContent().trim();
            }/*from  ww  w. j  a v a 2 s . c om*/
            String nodeWeiZhiTemp = nodeWeiZhi.replaceFirst(nodeNames[0], "").replaceFirst(">", "");
            NodeList childrenTempList = node.getChildNodes();
            if (childrenTempList.getLength() > 0 && !nodeWeiZhiTemp.equals("")) {
                result += getNodeText((Element) node, nodeWeiZhiTemp);
            }
        }
    }
    return result;
}

From source file:Main.java

public static List<Node> getChildrenByTagName(Node node, String element) {
    List<Node> result = new ArrayList<Node>();
    NodeList childNodes = node.getChildNodes();
    for (int j = 0; j < childNodes.getLength(); j++) {
        Node childNode = childNodes.item(j);
        if ("*".equals(element) || element == null || childNode.getNodeName().equals(element)) {
            result.add(childNode);//  w  w w.j ava2s. c  o m
        }
    }
    return result;
}

From source file:Main.java

/**
 * Finds all children of a node with the given name
 * @param parent the parent node/*from   w  ww.  j av a2s  . co m*/
 * @param name the name
 * @return the child nodes
 */
public static List<Node> findAllChildren(Node parent, String name) {
    List<Node> found = new ArrayList<Node>();
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i) {
        Node node = children.item(i);
        if (name.equals(node.getNodeName())) {
            found.add(node);
        }
    }
    return found;
}

From source file:Main.java

public static String getXmlString(Node inXml, String xpath) {
    if (!xpath.contains("/")) {
        NodeList elements = ((Element) inXml).getElementsByTagName(xpath);
        if (elements.getLength() == 1) {
            return elements.item(0).getTextContent();
        }/*from ww  w.j a  va2s  .  c o  m*/
    }

    try {
        return xPath.evaluate(xpath, inXml);
    } catch (Exception ex) {
        throw new RuntimeException("Could not run xpath: " + xpath, ex);
    }
}

From source file:Main.java

public static final List<Node> selectNodesViaXPath(XPath xPath, Node startingNode, String xPathExpression)
        throws XPathExpressionException {
    List<Node> data = new ArrayList<Node>();
    XPathExpression expression = xPath.compile(xPathExpression);
    Object result = expression.evaluate(startingNode, XPathConstants.NODESET);
    NodeList nodeList = (NodeList) result;
    for (int index = 0; index < nodeList.getLength(); index++) {
        data.add(nodeList.item(index));
    }//from  ww  w .j a  v a2s  .  c  om
    return data;
}

From source file:Main.java

/**
 * Get the value of a node./*w w w.  j a v  a2s .co m*/
 * 
 * @param node node
 * @return string
 */
public static String getValue(Node node) {
    NodeList children = node.getChildNodes();
    if (children.getLength() == 0) {
        return "";
    }
    return children.item(0).getNodeValue();
}

From source file:Main.java

public static String GetStringValueForNode(Node item) throws Exception {
    if (item instanceof Element) {
        StringBuilder builder = new StringBuilder();
        NodeList children = item.getChildNodes();
        for (int index = 0; index < children.getLength(); index++)
            builder.append(children.item(index).getNodeValue());

        // return...
        return builder.toString();
    } else/*from  w  w w  .jav a2 s .  c  o m*/
        throw new Exception(String.format("Cannot handle '%s'.", item.getClass()));
}

From source file:Main.java

/**
 * find elements by name.//from  ww  w.j  ava 2s  .co m
 * 
 * @param node - current node
 * @param name - name element
 * @param elements - list of found elements
 */
static public void findElementsByName(Node node, List<Node> elements, String name) {
    // get children
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node child = children.item(i);
        // if current child is required then add his to list
        if (name.equalsIgnoreCase((child.getNodeName()))) {
            elements.add(child);
        } else {
            findElementsByName(child, elements, name);
        }
    }
}

From source file:Main.java

/**
 * Method to check difference between actual time and saved time in xml file.
 * //from ww w .j  a  v  a2 s  .c o  m
 * @param target document
 * @return time difference as integer
 * @throws ParseException
 */
public static int checkDateDifference(Document target) throws ParseException {
    int diff = 0;
    NodeList parents = target.getElementsByTagName("g:options");
    Element parent = (Element) parents.item(0); //g:options - only 1 element
    DateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.ENGLISH);
    //date xml
    String time = parent.getAttribute("time");
    Date oldT = format.parse(time);
    //date now
    Date newT = new Date();
    //compare
    diff = (int) ((newT.getTime() - oldT.getTime()) / 1000);
    return diff;
}