Example usage for org.w3c.dom NodeList getLength

List of usage examples for org.w3c.dom NodeList getLength

Introduction

In this page you can find the example usage for org.w3c.dom NodeList getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in the list.

Usage

From source file:Main.java

public static Element GetCustomer(JFrame mainFrame, long lookupValue, Document CustomerDoc) {
    Element Customer = null;//from   w w w  . j  a  va 2 s.c  om
    Element Root = CustomerDoc.getDocumentElement();
    if (Root.hasChildNodes()) {
        NodeList Customers = Root.getChildNodes();
        for (int i = 0; i < Customers.getLength(); i++) {
            if (Customers.item(i).getNodeName() == "#text")
                continue;
            Element Current = (Element) Customers.item(i);
            if (lookupValue == Integer.parseInt(Current.getAttribute("Id"))) {
                Customer = Current;
            }
        }
    }
    return Customer;
}

From source file:Main.java

private static String getTextValue(String def, Element doc, String tag) {
    String value = def;/*from  ww  w  . j  a v  a  2s . c o  m*/
    NodeList noteList = doc.getElementsByTagName(tag);
    if (noteList.getLength() > 0) {
        for (int i = 0; i < noteList.getLength(); i++) {
            Node note = noteList.item(i);

            Element noteElement = (Element) note;
            if (noteElement.getNodeType() == Node.ELEMENT_NODE) {
                if (noteElement.getElementsByTagName("pitch").getLength() > 0) {
                    Element pitchElement = (Element) noteElement.getElementsByTagName("pitch").item(0);
                    System.out.println("Staff idX : "
                            + pitchElement.getElementsByTagName("step").item(0).getTextContent());
                } else {
                    System.out.println("yoktir");
                }
            } else {
                System.out.println("not element");
            }
        }

        //          value = nl.item(0).getFirstChild().getNodeValue();
    }
    return value;
}

From source file:Main.java

/**
 * Extract the node string representation.
 * //  w  w  w . j  a  va 2  s  .c  om
 * @param nodeList
 * @return
 */
public static String getXmlNodeAttribute(String attributeName, NodeList nodeList) {
    String retObj = null;

    if (nodeList != null && nodeList.getLength() > 0) {
        Element element = (Element) nodeList.item(0);
        retObj = element.getAttribute(attributeName);
    }
    return retObj;
}

From source file:Main.java

public static List<Element> readChildren(Node parentNode, String nodeName) {
    ArrayList<Element> ret = new ArrayList<Element>();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i).getNodeName().equals(nodeName))
            ret.add((Element) children.item(i));
    }/* ww  w  .j  av a 2 s . co m*/
    return ret;
}

From source file:Main.java

public static List<Element> readChildren(Node parentNode) {
    ArrayList<Element> ret = new ArrayList<Element>();
    NodeList children = parentNode.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        if (children.item(i) instanceof Element)
            ret.add((Element) children.item(i));
    }/*from w w  w.ja v  a  2 s  .  c  om*/
    return ret;
}

From source file:Main.java

private static Node getSingleNodeElementByXpath(String xpath) throws Exception {
    NodeList list = getNodeListByXpath(xpath);
    Node node = null;//  w ww  . j  a v a 2  s  .  co  m
    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 first child element with specified name.
 *
 * @param parentNode parent node.//from  w w  w.j  a v a2s.  c  o  m
 * @param childName  child name.
 * @return first childr element with specified name.
 */
public static Element getChildElement(final Node parentNode, final String childName) {
    //        parentNode.normalize();
    final NodeList nodeList = parentNode.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(childName))
            return (Element) node;
    }
    return null;
}

From source file:Main.java

private static String getStringValue(Element element, String tagName) {
    String textValue = null;//from ww  w.  j  a  va2s. c o  m
    NodeList nl = element.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        textValue = el.getFirstChild().getNodeValue();
    }
    return textValue;
}

From source file:Main.java

public static Element getChildElement(Element el, String tagName) {
    if (el == null)
        return null;
    NodeList list = el.getElementsByTagName(tagName);
    if (list != null && list.getLength() > 0) {
        return (Element) list.item(0);
    }// ww w. j av a  2 s. co m
    return null;
}

From source file:Main.java

public static Map<String, String> getChildElementNodesMap(Node node) {
    if (node == null) {
        return null;
    }/*from w  w  w.  j ava 2s. com*/

    Map<String, String> map = new ConcurrentHashMap<>();
    NodeList nodeList = node.getChildNodes();
    if (nodeList != null && nodeList.getLength() > 0) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node item = nodeList.item(i);
            if (item != null && item.getNodeType() == Node.ELEMENT_NODE) {
                map.put(item.getNodeName(), item.getTextContent().trim());
            }
        }
    }
    return map;
}