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 synchronized String getElementValue(Element element) {
    if (element == null) {
        return null;
    }/*from  ww w.j av  a2 s .c  om*/
    String retnStr = null;
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Text) {
            String str = node.getNodeValue();
            if (str == null || str.length() == 0 || str.trim().length() == 0) {
                continue;
            } else {
                retnStr = str;
                break;
            }
        }
    }
    return retnStr;
}

From source file:Main.java

public static synchronized String[] getElementValues(Element element) {
    if (element == null) {
        return null;
    }//from   w w w.j  a v a2s  . c  om
    Vector childs = new Vector();
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Text) {
            childs.add(node.getNodeValue());
        }
    }
    String[] values = new String[childs.size()];
    childs.toArray(values);
    return values;
}

From source file:Main.java

/**
 * Concat all the text and cdata node children of this elem and return
 * the resulting text./*from w w w  . jav  a 2s . co m*/
 * (by Matt Duftler)
 *
 * @param parentEl the element whose cdata/text node values are to
 *                 be combined.
 * @return the concatanated string.
 */
public static String getChildCharacterData(Element parentEl) {
    if (parentEl == null) {
        return null;
    }
    Node tempNode = parentEl.getFirstChild();
    StringBuffer strBuf = new StringBuffer();
    CharacterData charData;

    while (tempNode != null) {
        switch (tempNode.getNodeType()) {
        case Node.TEXT_NODE:
        case Node.CDATA_SECTION_NODE:
            charData = (CharacterData) tempNode;
            strBuf.append(charData.getData());
            break;
        }
        tempNode = tempNode.getNextSibling();
    }
    return strBuf.toString();
}

From source file:Main.java

public static Element getFirstElementChild(final Element element, final String namespace,
        final String localname) {
    Node node = element.getFirstChild();
    if (node == null) {
        return null;
    }//from   www  .  ja  v a  2 s  .co  m

    do {
        if (match(node, namespace, localname)) {
            return (Element) node;
        }
    } while ((node = node.getNextSibling()) != null);
    return null;
}

From source file:Main.java

public static String getChildElemText(Element parent, String tagName) {
    String val = null;
    Element childElem = getFirstChildByTagName(parent, tagName);
    if (childElem != null) {
        val = childElem.getFirstChild().getNodeValue();
    }//from   w w  w. j  a v a2  s .  c  om
    return val;
}

From source file:Main.java

/**
 * //from   w w w  . jav a 2  s.co  m
 * @param doc
 * @param strTagName
 * @return
 */
public static ArrayList<String> getArrayListFromNodes(Document doc, String strTagName) {

    ArrayList<String> alValues = new ArrayList<String>();

    NodeList nl = doc.getElementsByTagName(strTagName);

    // if there are tags
    if (nl != null) {

        // cycles on all of them
        for (int i = 0; i < nl.getLength(); i++) {

            // if it is a field Node
            if (nl.item(i).getNodeType() == 1) {

                // gets the i-th element
                Element e = (Element) nl.item(i);

                // if it has a value adds it to the AL
                if (e.getFirstChild() != null)
                    alValues.add(e.getFirstChild().getNodeValue());
            }
        }
    }

    return alValues;
}

From source file:Utils.java

/**
 * Return child elements with specified name.
 * /* ww  w . ja v a2  s .  c o m*/
 * @param parent
 * @param ns
 * @param localName
 * @return
 */
public static List<Element> getChildrenWithName(Element parent, String ns, String localName) {
    List<Element> r = new ArrayList<Element>();
    for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
        if (n instanceof Element) {
            Element e = (Element) n;
            String eNs = (e.getNamespaceURI() == null) ? "" : e.getNamespaceURI();
            if (ns.equals(eNs) && localName.equals(e.getLocalName())) {
                r.add(e);
            }
        }
    }
    return r;
}

From source file:Main.java

public static synchronized Element getChildElement(Element element) {
    if (element == null) {
        return null;
    }/* ww w.  j  a v  a 2s . c o  m*/
    Element childs = null;
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Element) {
            childs = (Element) node;
            break;
        }
    }
    return childs;
}

From source file:Main.java

public static String setInnerText(Element node, String value) {
    StringBuilder sb = new StringBuilder();
    while (node.hasChildNodes()) {
        Node tn = node.getFirstChild();
        if (tn.getNodeType() == Node.TEXT_NODE) {
            sb.append(tn.getNodeValue());
        }/*w  w  w . ja v a2  s.co m*/
        node.removeChild(tn);
    }
    node.appendChild(node.getOwnerDocument().createTextNode(value));
    return sb.toString();
}

From source file:Main.java

/**
 * Get all the direct children elements of an element.
 *
 * @param parent//from   ww  w.j a va 2  s  .co m
 *            The parent element.
 *
 * @return A list of Element's.
 */
public static List<Element> getElements(final Element parent) {
    final LinkedList<Element> list = new LinkedList<Element>();

    Node node = parent.getFirstChild();

    while (node != null) {
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            list.add((Element) node);
        }

        node = node.getNextSibling();
    }

    return list;
}