Example usage for org.w3c.dom Element getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:Main.java

public static Collection<Element> getChildElementListNS(Element parent, String nsURI) {
    List<Element> list = new ArrayList<>();
    for (org.w3c.dom.Node node = parent.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE && node.getNamespaceURI().equals(nsURI)) {
            list.add((Element) node);
        }//w w  w . ja  v  a 2s  .  com
    }
    return list;
}

From source file:XMLUtils.java

/**
 * Returns a list of value for the given node.
 * @param ns the namespace./*from   w w  w.j a va2s.c o m*/
 * @param base the element from where to search.
 * @param name of the element to get.
 * @return the list of value of this element.
 */
public static List<String> getStringListValueElement(final String ns, final Element base, final String name) {
    List<String> returnedlist = new ArrayList<String>();

    // Get element
    NodeList list = base.getElementsByTagNameNS(ns, name);
    int length = list.getLength();

    // Get all values of all elements
    if (length > 0) {
        for (int i = 0; i < length; i++) {
            Element element = (Element) list.item(i);
            Node node = element.getFirstChild();
            if (node != null) {
                returnedlist.add(node.getNodeValue());
            }
        }
    }
    return returnedlist;
}

From source file:Main.java

/**
 * Returns the value of given tagName under given element.
 * /*  www.ja  va2  s. c  o  m*/
 * E.g.
 * <ele>
 *    <tagName>MyValue</tagName>
 * </ele>
 * 
 * MyValue is returned in the above case.
 * 
 * @param ele xml element under which the tagName should be found
 * @param tagName tag name of the tag which value is read
 * @return the value of given tagName under given element.
 */
public static String getTextValue(Element ele, String tagName) {
    String textVal = null;

    try {
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            Element el = (Element) nl.item(0);
            Node firstChild = el.getFirstChild();
            if (firstChild != null)
                textVal = firstChild.getNodeValue();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return textVal;
}

From source file:Main.java

/**
 * Get the first child element from the input elment.
 * //  ww w . j av a2s.  co  m
 * @param element an Element object.
 * @return the firstchild element.
 */
public static Element getFirstChild(Element element) {
    // Return the first child element
    return findNextSibling(element.getFirstChild());
}

From source file:Main.java

/**
 * Returns the child text of this element, or the default value if there is none.
 * @param element/*from  w ww  .  j a  va  2  s.co m*/
 * @param defaultValue
 * @return Either the child text of the element or the default value if there is not child text.
 */
public static String getChildText(Element element, String defaultValue) {
    Node childNode = element.getFirstChild();
    if (childNode == null || !(childNode instanceof Text))
        return defaultValue;

    return childNode.getNodeValue();
}

From source file:Main.java

public static Collection<Element> getChildElementList(Element element) {
    List<Element> children = new ArrayList<>();
    for (org.w3c.dom.Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
            children.add((Element) node);
        }//from  w  ww.  j  ava 2s . c om
    }
    return children;
}

From source file:Main.java

public static Element getFirstChildElement(Element parentElem) {
    if (parentElem == null) {
        return null;
    }//  www  . j av a2  s  .  co m
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) child;
        }
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

/**
 * Obtains the text of the specified element.
 *
 * @param element the text element.//from   ww  w  .j a  v a 2s .  co  m
 * @return the text value inside the element.
 */
public static String getText(final Element element) {
    final Node node = element.getFirstChild();
    if (node != null && node.getNodeType() == Node.TEXT_NODE) {
        return node.getNodeValue().trim();
    }
    return null;
}

From source file:Main.java

/**
 * Returns the first child element with the specified name, or null if none exists.
 * @param element//w ww.j a va 2 s .  c o m
 * @param elementName
 * @return Element
 */
public static Element getChildElement(Element element, String elementName) {
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Element) {
            Element childElem = (Element) node;
            String elemName = getElementName(childElem);

            if (elementName.equals(elemName))
                return childElem;
        }
    }

    return null;
}

From source file:Main.java

static public String getNodeValue(Element parent, String nodeName) {
    NodeList nodes = parent.getElementsByTagName(nodeName);
    if (nodes.getLength() == 0)
        return null;
    Element curElem = (Element) nodes.item(0);
    Node textNode = curElem.getFirstChild();
    if (textNode != null) {
        String encVal = curElem.getAttribute("enc");
        String charSetVal = curElem.getAttribute("charSet");
        String returnVal = textNode.getNodeValue();
        if (encVal != null && encVal.equals("t")) {
            if (charSetVal == null)
                return (URLDecoder.decode(returnVal));
            else/*  w w w  . j  a  v a2 s. com*/
                try {
                    return (URLDecoder.decode(returnVal, charSetVal));
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        return (returnVal);
    } else
        return ("");
}