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 boolean hasChild(Node xml, String name) {
    NodeList nl = xml.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from   w  w  w .j  av  a 2s. c om
        if (name.equalsIgnoreCase(n.getNodeName()))
            return true;
    }
    return false;
}

From source file:Utils.java

/**
 * //  ww w .j  av  a  2  s .co  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

public static NodeList check1Most(NodeList node, String tag, boolean required) {
    if (node.getLength() == 0) {
        if (!required) {
            return null;
        }//from  w  w w. j  av a2  s.  com
        throw new RuntimeException("Tag " + tag + " is required in configuration file");
    } else if (node.getLength() > 1) {
        throw new RuntimeException("Only one tag " + tag + " is required in configuration file");
    }
    return node.item(0).getChildNodes();
}

From source file:Main.java

/**
 * Tranform a node list into a document fragment
 *//*from ww w .  j  a v  a 2 s  . c  o m*/
public static Node toDocumentFragment(NodeList list) {
    if (list.getLength() == 1 && list.item(0) instanceof Document)
        return list.item(0);

    Document document = newDocument();
    DocumentFragment fragment = document.createDocumentFragment();
    for (int i = 0; i < list.getLength(); i++)
        fragment.appendChild(document.adoptNode(list.item(i).cloneNode(true)));
    return fragment;
}

From source file:Main.java

public static void visitRecursively(Node node, BufferedWriter bw) throws Exception {
    NodeList list = node.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        // get child node
        Node childNode = list.item(i);
        if (childNode.getNodeType() == Node.TEXT_NODE) {
            System.out.println("Found Node: " + childNode.getNodeName() + " - with value: "
                    + childNode.getNodeValue() + " Node type:" + childNode.getNodeType());

            String nodeValue = childNode.getNodeValue();
            nodeValue = nodeValue.replace("\n", "").replaceAll("\\s", "");
            if (!nodeValue.isEmpty()) {
                System.out.println(nodeValue);
                bw.write(nodeValue);/*from w  w w . java2s .c  o m*/
                bw.newLine();
            }
        }
        visitRecursively(childNode, bw);
    }
}

From source file:Main.java

private static void cleanWhiteList(Node node, ArrayList<String> whiteList) {
    if (whiteList.contains(node.getLocalName())) {
        node.setNodeValue(null);/*from w ww . j a v  a 2s . c  o m*/
        node.setTextContent(null);
        // System.err.println("haha");
    }
    NodeList children = node.getChildNodes();
    if (children.getLength() != 0) {
        for (int i = 0; i < children.getLength(); i++)
            cleanWhiteList(children.item(i), whiteList);
    }

}

From source file:Main.java

public static final String getText(Node node) {
    if (node.hasChildNodes()) {
        NodeList childNodes = node.getChildNodes();
        if (childNodes.getLength() > 0) {
            Node child = childNodes.item(0);
            if ((child.getNodeType() == Node.CDATA_SECTION_NODE) || (child.getNodeType() == Node.TEXT_NODE)) {
                return child.getNodeValue();
            }/*w  w w.j  av a  2 s .c  om*/
        }
    }
    return null;
}

From source file:Main.java

public static void forEach(NodeList nl, Consumer<Node> action) {
    for (int i = 0; i < nl.getLength(); i++) {
        action.accept(nl.item(i));//from   w w w  .ja v a 2s.  c o m
    }
}

From source file:Main.java

public static void removeAllChildren(Element deps) {
    NodeList childNodes = deps.getChildNodes();

    for (int i = childNodes.getLength() - 1; i >= 0; i--) {
        Node item = childNodes.item(i);
        deps.removeChild(item);//from  w  w  w. ja v a  2s  .  c om
    }
}

From source file:Main.java

public static String getFirstElementText(Document parent, String tag) {
    NodeList nl = parent.getElementsByTagName(tag);
    if (nl.getLength() > 0) {
        return ((Element) nl.item(0)).getTextContent();
    }/*from  www  .j  a  v a 2  s . com*/
    return null;
}