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 List<Element> getChildElements(Element elt, String name) {
    List<Element> list = new ArrayList<Element>();

    NodeList nodes = elt.getChildNodes();
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        if (child.getNodeType() == Node.ELEMENT_NODE)
            if (name.equals(child.getNodeName()))
                list.add((Element) child);
    }/*from w  w  w  .  j a  v  a  2s .  c o  m*/
    return list;
}

From source file:Main.java

public static String getElementValue(Element element) {
    if (element != null) {
        NodeList nodes = element.getChildNodes();
        if (nodes != null && nodes.getLength() > 0) {
            for (int i = 0; i < nodes.getLength(); ++i) {
                Node node = nodes.item(i);
                if (node instanceof Text) {
                    return ((Text) node).getData();
                }/*from  w w w  .ja va 2s.c om*/
            }
        }
    }

    return null;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public final static Vector subElementList(Element superEle, String subName) {
    Vector v = new Vector();
    NodeList list = superEle.getChildNodes();
    if (list == null) {
        return null;
    }// w  w w.  ja v  a  2s.c  om

    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            if (node.getNodeName().equals(subName)) {
                v.add(node);
            }
        }
    }
    return v;
}

From source file:Main.java

/**
 * get single element by tag name/*from  w  w  w  .  ja v a2  s  . c om*/
 * @param element
 * @param tag
 * @return
 */
public static List<Element> getChildElements(Element element) {
    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;
            elems.add(e);
        }
    }
    return elems;
}

From source file:Main.java

public static void addAsFirstChild(Element element, Element newChild) {
    if (element.hasChildNodes()) {
        element.insertBefore(newChild, element.getChildNodes().item(0));
    } else {/*  w w w  .  j av  a 2  s.  c o  m*/
        element.appendChild(newChild);
    }
}

From source file:Main.java

public static String readProjectName(String file) throws Exception {
    DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
    DocumentBuilder dombuilder = domfac.newDocumentBuilder();
    InputStream is = new FileInputStream(file);
    Document doc = dombuilder.parse(is);
    Element root = doc.getDocumentElement();
    NodeList prjInfo = root.getChildNodes();
    if (prjInfo != null) {
        for (int i = 0; i < prjInfo.getLength(); i++) {
            Node project = prjInfo.item(i);
            if (project.getNodeType() == Node.ELEMENT_NODE) {
                String strProject = project.getNodeName();
                if (strProject.equals("project")) {
                    for (Node node = project.getFirstChild(); node != null; node = node.getNextSibling()) {
                        if (node.getNodeType() == Node.ELEMENT_NODE) {
                            String strNodeName = node.getNodeName();
                            if (strNodeName.equals("name")) {
                                return node.getTextContent();
                            }/*from  w  w w.j av  a2  s.  c  o  m*/
                        }
                    }
                }
            }

        }
    }
    return "";
}

From source file:Main.java

public static boolean isTextOnly(final Element element) {
    boolean isTextOnly = true;
    final NodeList nodeList = element.getChildNodes();
    for (int i = 0; i < nodeList.getLength() && isTextOnly; i++) {
        if (Element.class.isAssignableFrom(nodeList.item(i).getClass())) {
            isTextOnly = false;/*from ww  w .  ja  va 2  s.c  o  m*/
        }
    }
    return isTextOnly;
}

From source file:Main.java

public static List getChildren(Element parentEl, String name) {
    List children = new ArrayList();
    NodeList l = parentEl.getChildNodes();
    for (int i = 0; i < l.getLength(); i++) {
        Node n = l.item(i);/*  w w  w .  ja va2s .c  o  m*/
        if (Node.ELEMENT_NODE == n.getNodeType() && name.equals(n.getNodeName())) {
            children.add((Element) n);
        }
    }
    return children;
}

From source file:Main.java

/**
 * Gets all the text child nodes of an {@link Element} and coalesces them
 * together into one string./*from   w  w  w  . ja  v a  2 s.c  o m*/
 *
 * @param parent The {@link Element} to get the text child nodes of.
 * @return Returns the coalesced text.
 */
public static String getCoalescedTextChildrenOf(Element parent) {
    final StringBuilder buf = new StringBuilder();
    final NodeList children = parent.getChildNodes();
    final int numChildren = children.getLength();
    for (int i = 0; i < numChildren; ++i) {
        final Node child = children.item(i);
        if (child instanceof Text)
            buf.append(((CharacterData) child).getData());
    }
    return buf.toString();
}

From source file:Main.java

static public List<Element> getChildElementList(Element root) {
    List<Element> list = new ArrayList<>();
    NodeList nodes = root.getChildNodes();
    int i, size = nodes.getLength();
    Node node;/*w  w  w .j a va  2 s  . co  m*/

    for (i = 0; i < size; i++) {
        node = nodes.item(i);

        if (node.getNodeType() == Node.ELEMENT_NODE)
            list.add((Element) node);
    }

    return list;
}