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

/**
 * Return the first child Element with the given name; if name is null returns the first element.
 *///w ww.  ja v  a  2s  .  co  m
public static Element firstChildElement(Element element, String childElementName) {
    if (element == null)
        return null;
    // get the first element with the given name
    Node node = element.getFirstChild();

    if (node != null) {
        do {
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                Element childElement = (Element) node;

                return childElement;
            }
        } while ((node = node.getNextSibling()) != null);
    }
    return null;
}

From source file:Main.java

public static boolean updateTextNode(String nodeName, Element searchFrom, String data, int position)
        throws Exception {
    boolean result = false;
    Element currentElement = (Element) searchFrom.getElementsByTagName(nodeName).item(position);

    if (currentElement != null && currentElement.getNodeType() == Node.ELEMENT_NODE) {
        Text textNode = (Text) (currentElement.getFirstChild());
        textNode.setData(data);//w  ww. ja v  a  2s. c o  m

        if (textNode != null && textNode.getNodeValue() == null)
            result = false;
        else
            result = true;
    }
    return result;
}

From source file:fr.aliasource.webmail.server.proxy.client.http.DOMUtils.java

public static String getElementText(Element node) {
    Text txtElem = (Text) node.getFirstChild();
    if (txtElem == null) {
        return null;
    }//from   w  ww. j a v  a  2 s  . com
    return txtElem.getData();
}

From source file:Main.java

public static List<Node> contents(Element element) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }//  www .  j a v  a  2s.  co  m

    List<Node> contents = new ArrayList<Node>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        contents.add(child);
    }
    return contents;
}

From source file:Main.java

public static void setText(Element ele, String stext) {
    try {/*from  www . j a v  a  2  s.c  o  m*/
        if (null == ele)
            return;
        if (null == stext) {
            ele.appendChild(ele.getOwnerDocument().createTextNode(""));
        } else {
            ele.getFirstChild().setNodeValue(stext);
        }
    } catch (Exception e) {
        ele.appendChild(ele.getOwnerDocument().createTextNode(stext));
    }
}

From source file:Main.java

/**
 *
 * @param document//from   ww  w  . j av a 2s .c o m
 * @param tagName
 * @return
 */
public static String getTagValueAsString(Document document, String tagName) {
    if (document == null || tagName == null || tagName.length() == 0)
        return null;

    String tagValue = null;
    NodeList nlist = null;
    Element element = null;

    Element root = document.getDocumentElement();

    nlist = root.getElementsByTagName(tagName);
    element = (Element) nlist.item(0);
    if (element.hasChildNodes()) {
        tagValue = element.getFirstChild().getNodeValue();
    }
    return tagValue;
}

From source file:Main.java

public static List<Element> elements(Element element, String tagName) {
    if (element == null || !element.hasChildNodes()) {
        return Collections.emptyList();
    }/*from ww  w  .  ja  va 2  s  .c o  m*/

    List<Element> elements = new ArrayList<Element>();
    for (Node child = element.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            Element childElement = (Element) child;
            String childTagName = childElement.getNodeName();
            if (tagName.equals(childTagName))
                elements.add(childElement);
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Utility function for getting the first child element from a element
 *
 * @param element the parent element/*ww  w  .  j  a v  a  2  s.c  o m*/
 * @return the first child element if one was found, null otherwise
 */
public static Element getFirstChildElement(Element element) {
    if (element == null) {
        throw new IllegalArgumentException("nullArgument: element");
        //                    i18n.getMessage("nullArgument", "element"));
    }
    for (Node currentChild = element.getFirstChild(); currentChild != null; currentChild = currentChild
            .getNextSibling()) {
        if (currentChild.getNodeType() == Node.ELEMENT_NODE) {
            return (Element) currentChild;
        }
    }

    return null;
}

From source file:Main.java

public static synchronized Element[] getChildElements(Element element, String childName) {
    if (element == null || childName == null || childName.length() == 0) {
        return null;
    }//from  w w  w . ja v  a2s.  co m
    Vector childs = new Vector();
    for (Node node = element.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node instanceof Element) {
            childs.add((Element) node);
        }
    }
    Element[] elmt = new Element[childs.size()];
    childs.toArray(elmt);
    return elmt;
}

From source file:DOMEdit.java

public static void findByID(Document doc, String idName) {
    Element name = doc.getElementById(idName);
    if (name == null) {
        System.out.println("There is no element with the ID " + idName);
    } else {//from  w  ww .ja  va  2  s .co m
        Text text = (Text) name.getFirstChild();
        System.out.println("The ID " + idName + " locates the name " + text.getData());
    }
}