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 Node[] getChildrenNamed(Node node, String name) {
    ArrayList v = new ArrayList();

    NodeList nList = node.getChildNodes();
    for (int i = 0; i < nList.getLength(); i++) {
        Node n = nList.item(i);//from   w  ww . j  av  a2  s . c  om
        if (n.getNodeName().equalsIgnoreCase(name)) {
            v.add(n);
        }
    }

    Node[] nodeArray = new Node[v.size()];
    nodeArray = (Node[]) v.toArray(nodeArray);
    return nodeArray;
}

From source file:Main.java

public static boolean nodeExist(InputStream is, String xpathStr)
        throws SAXException, IOException, XPathExpressionException {
    boolean result = false;
    Document doc = null;/*  w w w  .  j  ava 2 s.  c  o  m*/
    if (is == null)
        return result;
    doc = dombuilder.parse(is);
    Object obj = xpath.compile(xpathStr).evaluate(doc, XPathConstants.NODESET);
    if (obj != null) {
        NodeList nodes = (NodeList) obj;
        if (nodes.getLength() > 0) {
            result = true;
        }
    }
    return result;
}

From source file:Main.java

public static Node findByID(Document doc, String id, String tagName) {
    NodeList node = doc.getElementsByTagName(tagName);
    for (int i = 0; i < node.getLength(); i++) {
        NamedNodeMap attributtes = node.item(i).getAttributes();
        for (int j = 0; j < attributtes.getLength(); j++)
            if (id.equalsIgnoreCase(attributtes.item(j).getNodeValue()))
                return node.item(i);
    }//from   www.ja  v a 2s. c  o  m

    return null;
}

From source file:Main.java

public static Element[] getChildrenByName(Element parentElement, String childrenName) {
    NodeList nl = parentElement.getChildNodes();
    int max = nl.getLength();
    LinkedList<Node> list = new LinkedList<Node>();
    for (int i = 0; i < max; i++) {
        Node n = nl.item(i);//w  w  w.j a va  2  s. com
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childrenName)) {
            list.add(n);
        }
    }
    return list.toArray(new Element[list.size()]);
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;//from w w w  .ja v a  2 s.  c  o  m
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        if (el == null)
            return null;
        if (el.getFirstChild() == null)
            return null;
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:Utils.java

public static Element findContainerElseCreate(Document document, Element parent, String child) {
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }/*  www  .j ava2s. c  om*/
    return (Element) parent.getElementsByTagName(child).item(0);
}

From source file:Main.java

public static String getTextContent(Element elm) {
    if (elm == null) {
        return null;
    }/*from  ww w . j  av a 2  s  .co  m*/
    StringBuilder result = new StringBuilder();
    NodeList childNodes = elm.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if ((node == null) || (node.getNodeType() != Node.TEXT_NODE)) {
            continue;
        }
        result.append(((Text) node).getData());
    }
    return result.toString().trim();
}

From source file:Main.java

/**
 * Returns the inner value of the named node in the given document.  The node must only exist once
 *
 * @param doc the XML document//from w  w w.java 2  s  .c om
 * @param nodeName the name of the node
 * @return the value of the named node
 */
public static String getNodeInnerValue(final Document doc, final String nodeName) throws SAXException {
    final NodeList nodes = doc.getElementsByTagName(nodeName);
    if (nodes.getLength() != 1) {
        throw new SAXException("Could not get single node for " + nodeName);
    }
    final NodeList childNodes = nodes.item(0).getChildNodes();
    if (childNodes.getLength() != 1) {
        throw new SAXException("Could not get single child node for " + nodeName);
    }
    return childNodes.item(0).getNodeValue();
}

From source file:Main.java

public static Element getFirstChildElement(Node parent) {
    NodeList children = parent.getChildNodes();
    for (int i = 0; i < children.getLength(); ++i)
        if (children.item(i) instanceof Element)
            return (Element) children.item(i);
    return null;/*from   w w w  .j av a  2  s .co  m*/
}

From source file:Main.java

/**
 * Extract the text symbol from the given DOM element, ignoring XML comments.
 * <p>Appends all CharacterData nodes and EntityReference nodes
 * into a single String symbol, excluding Comment nodes.
 * @see CharacterData//  w  w  w  .j  a  va2  s. c om
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    StringBuffer value = new StringBuffer();
    NodeList nl = valueEle.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if (item instanceof CharacterData && !(item instanceof Comment) || item instanceof EntityReference)
            value.append(item.getNodeValue());
    }
    return value.toString();
}