Example usage for org.w3c.dom Node hasChildNodes

List of usage examples for org.w3c.dom Node hasChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom Node hasChildNodes.

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:Main.java

private static Node actualFindNode(Node node, String name) {

    String nodeName = node.getNodeName();
    nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));

    if (nodeName.equals(name)) {
        return node;
    }//  w  w w. j a  v  a2 s  .c  o  m
    if (node.hasChildNodes()) {
        NodeList list = node.getChildNodes();
        int size = list.getLength();

        for (int i = 0; i < size; i++) {
            Node found = actualFindNode(list.item(i), name);
            if (found != null)
                return found;
        }
    }
    return null;
}

From source file:Main.java

protected static void writeXMLwalkTree(Node node, int indent, PrintWriter out) {
    if (node == null)
        throw new NullPointerException("Null node passed to writeXMLwalkTree()");
    if (node.hasChildNodes()) {
        if (node instanceof Element) {
            Element elem = (Element) node;
            //elem.normalize();
            out.print("\n");
            for (int j = 0; j < indent; j++) {
                out.print(" ");
            }/*from  w ww.  java 2 s .  c o m*/
            out.print("<" + elem.getTagName());
            NamedNodeMap attrs = elem.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr a = (Attr) attrs.item(i);
                out.print(" " + a.getName() + "=\"" + a.getValue() + "\"");
            }
            out.print(">");
            NodeList nl = node.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                writeXMLwalkTree(nl.item(i), indent + 2, out);
            }
            //              for(int j=0;j<indent;j++) {
            //                  out.print(" ");
            //              }
            out.println("</" + elem.getTagName() + ">");
        }
    } else {
        if (node instanceof Element) {
            Element elem = (Element) node;
            out.print("\n");
            for (int j = 0; j < indent; j++) {
                out.print(" ");
            }
            out.print("<" + elem.getTagName());
            NamedNodeMap attrs = elem.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Attr a = (Attr) attrs.item(i);
                out.print(" " + a.getName() + "=\"" + a.getValue() + "\"");
            }
            out.println("/>");
        } else if (node instanceof CDATASection) {
            CDATASection cdata = (CDATASection) node;
            //              for(int j=0;j<indent;j++) {
            //                  out.print(" ");
            //              }
            out.print("<![CDATA[" + cdata.getData() + "]]>");
        } else if (node instanceof Text) {
            Text text = (Text) node;
            StringBuilder buf = new StringBuilder(text.getData().length());
            for (int i = 0; i < text.getData().length(); i++) {
                if (text.getData().charAt(i) == '\n' || text.getData().charAt(i) == '\r'
                        || text.getData().charAt(i) == ' ' || text.getData().charAt(i) == '\t') {
                    if (buf.length() > 0 && buf.charAt(buf.length() - 1) != ' ') {
                        buf.append(' ');
                    }
                } else {
                    buf.append(text.getData().charAt(i));
                }
            }
            if (buf.length() > 0 && !buf.toString().equals(" ")) {
                StringBuilder buf2 = new StringBuilder(buf.length() + indent);
                //                  for(int j=0;j<indent;j++) {
                //                      buf2.append(' ');
                //                  }
                buf2.append(buf.toString());
                out.print(buf2);
            }
        }
    }
}

From source file:Main.java

static private void getMatchingNodes(Node node, String[] nodePath, int cur, List<Node> res) {
    if (cur < 0 || cur >= nodePath.length)
        return;//from www  . ja v a  2  s  .c  o  m
    boolean last = (cur == nodePath.length - 1);
    String name = nodePath[cur];
    if (node.hasChildNodes()) {
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node c = children.item(i);
            if (name.equals(c.getNodeName())) {
                if (last) {
                    res.add(c);
                } else {
                    getMatchingNodes(c, nodePath, cur + 1, res);
                }
            }
        }
    }
}

From source file:Main.java

/**
 * Gets Nodes identical to a given test Node for a given parent Node
 *///www  . j a  v  a  2  s . c  o  m

public static List getSimilarChildNodes(Node parent, Node testNode, boolean ignoreWhitespace)

{

    ArrayList nodes = new ArrayList();

    if (!parent.hasChildNodes())

        return nodes;

    NodeList childNodes = parent.getChildNodes();

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

    {

        if (nodesSimilar(childNodes.item(i), testNode, ignoreWhitespace))

            nodes.add(childNodes.item(i));

    }

    return nodes;

}

From source file:Main.java

static String stringElement(Element row, String name) {
    final NodeList childNodes = row.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        final Node node = childNodes.item(i);
        if (name.equals(node.getLocalName())) {
            String result = node.getTextContent();
            // If content is not plain text then returns name of
            // first child tag
            if (result == null && node.hasChildNodes()) {
                result = node.getFirstChild().getLocalName();
            }//from  ww  w  .ja v  a2s . com
            return result;
        }
    }
    return null;
}

From source file:ua.kiev.doctorvera.utils.SMSGateway.java

public static final String getElementValue(Node elem) {
    Node child;/*from   w w  w . j  a v a  2 s .c o m*/
    if (elem != null) {
        if (elem.hasChildNodes()) {
            for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                if (child.getNodeType() == Node.TEXT_NODE) {
                    return child.getNodeValue();
                }
            }
        }
    }
    return "";
}

From source file:ee.ria.xroad.proxyui.WSDLParser.java

private static String getValue(Node node) {
    if (node.hasChildNodes()) {
        String value = node.getFirstChild().getNodeValue();

        if (value != null) {
            value = value.trim();//w ww .j  ava  2  s.  com
        }

        return value;
    }

    return null;
}

From source file:Main.java

public static void collectXpathContainText(Node node, String textContent, List<String> holder) {
    if (textContent.equals(node.getTextContent())) {
        String xpath = getXPath(node);
        if (!holder.contains(xpath)) {
            holder.add(xpath);//w  ww. j a  v  a2s  . c o m
        }
    }

    if (node.hasChildNodes()) {
        Node child = node.getFirstChild();
        while (child != null) {
            collectXpathContainText(child, textContent, holder);
            child = child.getNextSibling();
        }
    }
}

From source file:Main.java

public static Node findNode(Node node, String name) {
    String nodeName = node.getNodeName();
    nodeName = nodeName.substring((nodeName.indexOf(":") != -1 ? nodeName.indexOf(":") + 1 : 0));
    name = name.substring((name.indexOf(":") != -1 ? name.indexOf(":") + 1 : 0));
    if (nodeName.equals(name)) {
        return node;
    }//from  w w  w  . j  a v  a  2s. c  om
    if (node.hasChildNodes()) {
        NodeList list = node.getChildNodes();
        int size = list.getLength();

        for (int i = 0; i < size; i++) {
            Node found = findNode(list.item(i), name);
            if (found != null)
                return found;
        }
    }
    return null;
}

From source file:Main.java

public static void parsePseudoHTML(Node e, StringBuffer html) {
    String nodeName = e.getNodeName();
    boolean toAppendTag = !(e instanceof Text);
    boolean isBR = nodeName.equals("BR");
    if (toAppendTag & !isBR) {
        html.append('<');
        html.append(nodeName);/* ww  w .j  a  va 2 s .c  o  m*/
        html.append('>');
    }
    if (!e.hasChildNodes() && !isBR) {
        html.append(e.getNodeValue());
    } else {
        NodeList nodes = e.getChildNodes();
        int children = nodes.getLength();
        for (int i = 0; i < children; i++)
            parsePseudoHTML(nodes.item(i), html);
    }
    if (toAppendTag) {
        if (isBR) {
            html.append("<BR>");
        } else {
            html.append("</");
            html.append(nodeName);
            html.append(">\n");
        }
    }
}