Example usage for org.w3c.dom Element getChildNodes

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

Introduction

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

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuffer sb = new StringBuffer();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);//from   ww  w  .j ava 2 s. co m
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

private static void removeEmptyChildElements(Element parentElement) {
    List<Element> toRemove = new LinkedList<Element>();

    NodeList children = parentElement.getChildNodes();
    int childrenCount = children.getLength();
    for (int i = 0; i < childrenCount; ++i) {
        Node child = children.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            removeEmptyChildElements(childElement);
            if (elementIsRedundant(childElement)) {
                toRemove.add(childElement);
            }/*from ww  w .ja  va 2  s  .c om*/
        }
    }

    for (Element childElement : toRemove) {
        parentElement.removeChild(childElement);
    }
    parentElement.normalize();
}

From source file:Main.java

public static Element getFirstChildElementByTagName(Element element, String name) {
    boolean found = false;
    Element result = null;//w ww. j  a  va  2 s  .com
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength() && !found; i++) {
        if (nl.item(i) instanceof Element) {
            if (name.equals(nl.item(i).getNodeName())) {
                result = (Element) nl.item(i);
                found = true;
            }
        }

    }
    return result;
}

From source file:Main.java

/**
 * Extracts a bunch of notes from an <code>Element</code>
 *//*  w  ww  .  j  a  v  a  2s .  c  o m*/
protected static List<String> extractNotesFrom(Element element) {
    List<String> list = new ArrayList<String>();

    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (!(node instanceof Element)) {
            continue;
        }

        Element child = (Element) node;
        if (child.getTagName().equals("note")) {
            list.add(extractTextFrom(child));
        }
    }

    return list;
}

From source file:Main.java

public static final String getCDATA(Element elem, boolean trim) {
    StringBuilder sb = new StringBuilder();
    NodeList nl = elem.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node nc = nl.item(i);/*from www .  j a v a 2s.  c om*/
        if (nc.getNodeType() == Node.CDATA_SECTION_NODE) {
            sb.append(((Text) nc).getData());
        } else if (nc.getNodeType() == Node.TEXT_NODE) {
            String txt = ((Text) nc).getData();
            if (trim) {
                txt = txt.trim();
            }
            sb.append(txt);
        }
    }
    return sb.toString();
}

From source file:Main.java

public static Node[] getChildNodesByName(Element parent, String name) {
    List<Node> nodeList = new ArrayList<Node>();

    NodeList childNodes = parent.getChildNodes();
    int length = childNodes.getLength();
    for (int i = 0; i < length; i++) {
        Node current = childNodes.item(i);
        if (current.getNodeName().equals(name))
            nodeList.add(current);/*from   ww  w  .  j ava  2s .  c  o m*/
    }

    Node[] nodes = new Node[nodeList.size()];
    nodeList.toArray(nodes);
    return nodes;
}

From source file:Main.java

@Nonnull
public static List<Element> getChildrenElements(@Nonnull Element element, @Nonnull String childElementName) {
    NodeList childElts = element.getChildNodes();
    List<Element> result = new ArrayList<>();

    for (int i = 0; i < childElts.getLength(); i++) {
        Node node = childElts.item(i);
        if (node instanceof Element && node.getNodeName().equals(childElementName)) {
            result.add((Element) node);
        }/*from w w  w  .ja  v  a  2 s.  c o m*/
    }

    return result;
}

From source file:Main.java

public static Element[] getChildren(Element parent, String name) {
    ArrayList<Element> al = new ArrayList<Element>();
    NodeList nl = parent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from  w  w w .  j a v a  2 s .co  m
        if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) {
            al.add((Element) n);
        }
    }
    return al.toArray(new Element[0]);
}

From source file:Main.java

/**
 * get single element by tag name/*w  ww.j  a  v  a  2  s  . c o  m*/
 * @param element
 * @param tag
 * @return
 */
public static List<Element> getElementsByTagName(Element element, String tag) {
    List<Element> elems = new ArrayList<Element>();
    NodeList list = element.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(tag)) {
                elems.add(e);
            }
        }
    }
    return elems;
}

From source file:Utils.java

/**
 * <p>Returns a list of child elements with the given
 * name. Returns an empty list if there are no such child
 * elements.</p>/*from   w w  w  . ja  va  2 s.  co m*/
 *
 * @param parent parent element
 * @param name name of the child element
 * @return child elements
 */
public static List getChildElementsByName(Element parent, String name) {
    List elements = new ArrayList();

    NodeList children = parent.getChildNodes();

    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element element = (Element) node;
            if (element.getTagName().equals(name)) {
                elements.add(element);
            }
        }
    }

    return elements;
}