Example usage for org.w3c.dom Element hasChildNodes

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

Introduction

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

Prototype

public boolean hasChildNodes();

Source Link

Document

Returns whether this node has any children.

Usage

From source file:Main.java

public static void parseXMLDoc(Element element, Document doc, Map<String, String> oauthResponse) {
    NodeList child = null;/*  w w  w  .  j  a v  a2  s.  c  o m*/
    if (element == null) {
        child = doc.getChildNodes();

    } else {
        child = element.getChildNodes();
    }
    for (int j = 0; j < child.getLength(); j++) {
        if (child.item(j).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            Element childElement = (Element) child.item(j);
            if (childElement.hasChildNodes()) {
                System.out.println(childElement.getTagName() + " : " + childElement.getTextContent());
                oauthResponse.put(childElement.getTagName(), childElement.getTextContent());
                parseXMLDoc(childElement, null, oauthResponse);
            }

        }
    }
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }//from w w w  .  j a va 2s  .  c om

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getNodeName();
            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

private static boolean elementIsRedundant(Element element) {
    if (element.hasAttributes())
        return false;
    if (!element.hasChildNodes())
        return true;
    NodeList children = element.getChildNodes();
    int childrenCount = children.getLength();
    for (int i = 0; i < childrenCount; ++i) {
        Node child = children.item(i);
        String value = child.getNodeValue();
        if (value != null && !value.matches("\\s*")) {
            return false; // Found non-whitespace text
        }/*from w w w. ja  va2 s  . c om*/
    }
    return true;
}

From source file:Main.java

public static String setInnerText(Element node, String value) {
    StringBuilder sb = new StringBuilder();
    while (node.hasChildNodes()) {
        Node tn = node.getFirstChild();
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        }//  ww w .  ja  v  a2  s  .  c  o m
        node.removeChild(tn);
    }
    node.appendChild(node.getOwnerDocument().createTextNode(value));
    return sb.toString();
}

From source file:Main.java

public static List<Element> elementsQName(Element element, Set<QName> allowedTagNames) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/* w ww.j  a  v a 2s.c o  m*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            QName childQName = new QName(childElement.getNamespaceURI(), childElement.getLocalName());
            if (allowedTagNames.contains(childQName)) {
                elements.add(childElement);
            }
        }
    }
    return elements;
}

From source file:Main.java

public static String getPseudoHTML(Element xmlElement) {
    StringBuffer html = new StringBuffer();
    if (xmlElement.hasChildNodes()) {
        NodeList nodes = xmlElement.getChildNodes();
        int children = nodes.getLength();
        for (int i = 0; i < children; i++)
            parsePseudoHTML(nodes.item(i), html);
    }/*from  w ww  .  j a va 2  s .  com*/
    return html.toString();
}

From source file:Main.java

public static List<Element> elements(Element element) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }//  w w w .  jav a2 s .c  om

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            elements.add((Element) child);
        }
    }
    return elements;
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/* w w w  . j a  v a 2s.c  o  m*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getLocalName();

            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

public static Element findSingleElement(Element parent, String fullXPath) {
    Element elt = null;/*w  w  w  .  j  ava 2s.c om*/
    if (parent != null) {
        if (parent.hasChildNodes()) {
            if (fullXPath.startsWith("/"))
                fullXPath = fullXPath.substring(1);
            int index = fullXPath.indexOf("/");
            String childName = ((index != -1) ? fullXPath.substring(0, index) : fullXPath);

            NodeList list = parent.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node child = list.item(i);
                if (child.getNodeType() == Node.ELEMENT_NODE) {
                    if (child.getNodeName().equalsIgnoreCase(childName)) {
                        if (index == -1) {
                            elt = (Element) child;
                            break;
                        } else {
                            fullXPath = fullXPath.substring(index + 1);
                            elt = findSingleElement((Element) child, fullXPath);
                        }
                    }
                }
            }
        }
    }
    return elt;
}

From source file:Main.java

public static void setElementTextValue(Element e, String text, boolean cdata) {
    Document root = e.getOwnerDocument();
    e.normalize();//from   w w w. java 2 s  . c  om
    if (e.hasChildNodes()) {
        NodeList nl = e.getChildNodes();

        /* This suxx: NodeList Object is changed when removing children !!!
           I will store all nodes that should be deleted in a Vector and delete them afterwards */
        int length = nl.getLength();

        List<Node> v = new ArrayList<Node>(nl.getLength());
        for (int i = 0; i < length; i++)
            if (nl.item(i) instanceof CharacterData)
                v.add(nl.item(i));
        for (Node n : v)
            e.removeChild(n);
    }

    if (cdata) {
        e.appendChild(root.createCDATASection(text));
    } else {
        e.appendChild(root.createTextNode(text));
    }
}