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

/**
 * Method getFullTextChildrenFromElement
 *
 * @param element/* ww w . j  a  v  a  2 s .  com*/
 * @return the string of children
 */
public static String getFullTextChildrenFromElement(Element element) {
    StringBuilder sb = new StringBuilder();

    Node child = element.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.TEXT_NODE) {
            sb.append(((Text) child).getData());
        }
        child = child.getNextSibling();
    }

    return sb.toString();
}

From source file:Main.java

public static List elements(Element element) {
    ArrayList elements = new ArrayList();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element)
            elements.add(child);//  w  ww  .  j  ava2s  . co  m
    }
    return elements;
}

From source file:Utils.java

public static Element findElementAndSetElseCreateAndSet(Document document, Element parent, String child,
        String value) {/*  w w  w.  j  a va  2 s. c  o m*/
    NodeList nl = parent.getElementsByTagName(child);
    if (nl.getLength() == 0) {
        parent.appendChild(document.createElement(child));
    }
    Element ret = (Element) parent.getElementsByTagName(child).item(0);
    if (ret.getFirstChild() != null) {
        ret.removeChild(ret.getFirstChild());
    }
    ret.appendChild(document.createTextNode(value));
    return ret;
}

From source file:Main.java

/**
 * This method returns a list of the direct element node children of this element node with the specified tag.
 * @param node - parent node//from  ww w  .  ja v  a2s  .co  m
 * @param tag - tag of direct children to be returned
 * @return a list containing the direct element children with the given tag
 * @author Tristan Bepler
 */
public static List<Element> getDirectChildElementsByTag(Element node, String tag) {
    List<Element> children = new ArrayList<Element>();
    Node child = node.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE
                && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) {
            children.add((Element) child);
        }
        child = child.getNextSibling();
    }
    return children;
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;//from www.j a va  2  s.  c o  m
    Element el = getChildElement(ele, tagName);

    if (el != null)
        textVal = el.getFirstChild().getNodeValue();

    return textVal;
}

From source file:Main.java

public static Element getChildElementByLocalName(Element parentElem, String localName) {
    Node child = parentElem.getFirstChild();
    while (child != null) {
        if ((child.getNodeType() == Node.ELEMENT_NODE) && getLocalName(child).equals(localName)) {
            return (Element) child;
        }/*  w w  w  .  j  ava  2  s .c o  m*/
        child = child.getNextSibling();
    }
    return null;
}

From source file:Main.java

public static Element findChildElement(Element parent, String name) {
    if (parent == null) {
        return null;
    }//from w w w .j ava  2  s  .  com
    org.w3c.dom.Node ret = parent.getFirstChild();
    while (ret != null && (!(ret instanceof Element) || !ret.getNodeName().equals(name))) {
        ret = ret.getNextSibling();
    }
    return (Element) ret;
}

From source file:Main.java

public static List getDirectChildElements(Element p_rootElement, String p_elementName) {
    List result = new ArrayList();
    for (Node node = p_rootElement.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node.getNodeName().equalsIgnoreCase(p_elementName))
            result.add((Element) node);
    }/* w  w  w .  j  av  a  2 s  . c o  m*/
    return result;
}

From source file:XMLUtils.java

/**
 * Returns the value of the given node.// www  .  j a  v  a  2 s.c om
 * @param base the element from where to search.
 * @param name of the element to get.
 * @return the value of this element.
 */
public static String getStringValueElement(final Element base, final String name) {
    String value = null;

    // Get element
    NodeList list = base.getElementsByTagName(name);
    if (list.getLength() == 1) {
        Element element = (Element) list.item(0);
        Node node = element.getFirstChild();
        if (node != null) {
            value = node.getNodeValue();
        }
    } else if (list.getLength() > 1) {
        throw new IllegalStateException("Element '" + name + "' on '" + base
                + "' should be unique but there are '" + list.getLength() + "' elements");
    }

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

From source file:Main.java

/**
 * Returns a list of the direct {@link Element} children of the given element.
 *
 * @param parent//from   w w  w  .  j av  a 2 s .c om
 *            The {@link Element} to get the children from.
 * @return A {@link LinkedList} of the children {@link Element}s.
 */
public static LinkedList<Element> getDirectChildren(Element parent) {
    LinkedList<Element> list = new LinkedList<>();
    for (Node child = parent.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element) {
            list.add((Element) child);
        }
    }
    return list;
}