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

/**
 * get single element by tag name./*w w  w  .  ja v  a  2  s  .c  o  m*/
 *
 * @param element the element
 * @param tag the tag
 * @return the child elements
 */
public static List<Element> getChildElements(Element element, String tag) {
    return toElements(element.getChildNodes(), tag);
}

From source file:Main.java

/**
 * Retrieves the child element with the specified tag name for the given element.
 *
 * @param   elem     The element whose child element is to be retrieved.
 * @param   tagName  Name of the child element.
 * @pre     elem != null && tagName != null && !tagName.equals("")
 * @return  The text contents of the specified sub element, or null if the subelement
 *          does not exist./*from w  ww. j a  va  2 s  .c  o  m*/
 */
public static Element getChildElement(Element elem, String tagName) {
    NodeList children = elem.getChildNodes();
    Element childElement = null;
    int index = 0;
    while (childElement == null && index < children.getLength()) {
        Node child = children.item(index);
        if (child.getNodeType() == Node.ELEMENT_NODE && ((Element) child).getTagName().equals(tagName)) {
            childElement = (Element) child;
        } else {
            index++;
        }
    }
    return childElement;
}

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();
            }/* w  w w  . j a v a  2  s . c  om*/
            return result;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Extract the node string representation.
 * //  w  ww. j av a  2 s  .co  m
 * @param nodeList
 * @return
 */
public static String getXmlNodeValue(NodeList nodeList) {
    String retObj = null;

    if (nodeList != null && nodeList.getLength() > 0) {
        Element element = (Element) nodeList.item(0);
        if (element != null && element.getChildNodes() != null) {
            NodeList childNodes = element.getChildNodes();
            if (childNodes != null && childNodes.getLength() > 0) {
                retObj = ((Node) childNodes.item(0)).getNodeValue();
            }
        }
    }
    return retObj;
}

From source file:Main.java

static Element findChild(Element element, String ns, String tag) {
    final NodeList childNodes = element.getChildNodes();
    for (int i = 0; i < childNodes.getLength(); i++) {
        if (childNodes.item(i) instanceof Element) {
            Element child = (Element) childNodes.item(i);
            if (child.getLocalName().equals(tag) && (ns == null || child.getNamespaceURI().equals(ns))) {
                return child;
            }//from ww w  .java  2 s.c o  m
        }
    }
    return null;
}

From source file:Main.java

private static void getNodeList(Document doc, String nodeName, List<Node> result, Node node) {
    NodeList children;//from   www .  ja  v a2s.c o m
    if (node == null) {
        Element root = doc.getDocumentElement();
        children = root.getChildNodes();
    } else {
        if (node.getNodeName().equals(nodeName)) {
            result.add(node);
            return;
        }
        children = node.getChildNodes();
    }
    if (children == null)
        return;
    for (int i = 0; i < children.getLength(); i++) {
        getNodeList(doc, nodeName, result, children.item(i));
    }
}

From source file:Main.java

/**
 * Set the text of the specified element to the given string.
 * //from  w w  w  .  j  av  a 2  s . co m
 * @param e The element.
 * @param text The text string.
 */
public static void setText(Element e, String text) {
    NodeList lst = e.getChildNodes();
    int size = lst.getLength();
    for (int i = 0; i < size; i++) {
        Node n = lst.item(i);
        if (n.getNodeType() == Node.TEXT_NODE) {
            Text t = (Text) n;
            t.setData(text.trim());
            return;
        }
    }
    Document doc = e.getOwnerDocument();
    // bit of a hack - we preserve the cdata on the way in so we can serialize correctly
    // This only works on xml to xml 
    // TODO need to have a "preserve format" or some such on the mdmi structure
    if (text.startsWith("<![CDATA[") && text.endsWith("]]>")) {
        CDATASection cdata = doc
                .createCDATASection(text != null ? text.substring(9, text.lastIndexOf("]]>")) : null);
        e.appendChild(cdata);
    } else {
        Text txt = doc.createTextNode(text != null ? text.trim() : null);
        e.appendChild(txt);
    }

}

From source file:Main.java

/**
 * Get the text content of an element. If the element contains
 * mixed content (both elements and text), only the first text section
 * is returned.//ww w.  j a  v  a2  s  .com
 *
 * @param element target element to retrieve text on, cannot be null.
 * @return text content of the element.
 */
public static String getElementText(Element element) {
    element.normalize();
    NodeList list = element.getChildNodes();
    int len = list.getLength();

    for (int i = 0; i < len; i++) {
        Node n = list.item(i);

        if (n.getNodeType() == Node.TEXT_NODE) {
            String s = n.getNodeValue();

            if (s != null) {
                return s.trim();
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * This will get the text value of an element.
 *
 * @param node The node to get the text value for.
 * @return The text of the node./*from  ww  w .jav a2 s .  c om*/
 */
public static String getNodeValue(Element node) {
    String retval = "";
    NodeList children = node.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node next = children.item(i);
        if (next instanceof Text) {
            retval = next.getNodeValue();
        }
    }
    return retval;
}

From source file:Main.java

public static Element findChildElement(Element parent, String name) {
    Element result = null;/* w  w w  .j a v a  2s .  c o m*/
    NodeList nodes = parent.getChildNodes();

    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);

        if (node.getNodeName().equals(name)) {
            result = (Element) node;
            break;
        }
    }

    return result;
}