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

/**
 * get String Data/*from w ww .ja v a 2 s .com*/
 * @param  xmlElement an <CODE>Element</CODE> of XML document
 * @return String representing contents of <CODE>xmlElement</CODE>
 */
public static String getString(Element xmlElement) {
    Node node = xmlElement.getFirstChild();
    if (node == null)
        return "";
    if (node instanceof Text) {
        Text text = (Text) node;
        if (text.getData() == null)
            return "";
        else
            return text.getData().trim();
    }
    return getPseudoHTML(xmlElement);
}

From source file:Main.java

/**
 * Get the text value for the specified element.  If the element is null, or the element's body is empty then this
 * method will return null.//from w  w  w .  ja  v  a2s  . com
 *
 * @param element The Element
 * @return The value String or null
 */
public static String getValue(Element element) {
    if (element != null) {
        Node dataNode = element.getFirstChild();
        if (dataNode != null) {
            return ((Text) dataNode).getData();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Gets the value from the first descendant of a given ancestor, where the
 * descendant has the specified name.// w  w  w. jav a2s .co m
 * 
 * @param ancestor
 *            the ancestor element
 * @param tagname
 *            the tag name of the descendant to find.
 * @return the value of the descendant or <code>null</code> if no descendant
 *         with that name could be found or the descendant found has no
 *         value.
 */
public static String getValueFromDescendant(Element ancestor, String tagname) {
    if (ancestor != null) {
        NodeList nl = ancestor.getElementsByTagName(tagname);
        if (!isEmpty(nl)) {
            Element e = (Element) nl.item(0);
            Node c = e.getFirstChild();
            if (c != null) {
                return c.getNodeValue();
            }
        }
    }
    return null;
}

From source file:Main.java

public static String getElementValue(Element parent, String label) {
    Element e = (Element) parent.getElementsByTagName(label).item(0);

    try {/* w  w w. j av a 2 s. c  om*/
        Node child = e.getFirstChild();
        if (child instanceof CharacterData) {
            CharacterData cd = (CharacterData) child;
            return cd.getData();
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:Main.java

public static List<Element> getElementChildsByType(Element elem, String type) {
    List<Element> elemList = new ArrayList<Element>();
    for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && (((Element) child).getAttribute("xmi:type").equals(type)
                || ((Element) child).getAttribute("type").equals(type))) {
            elemList.add((Element) child);
        }/*from  ww w.j  a v a2  s .c  om*/
    }
    return elemList;
}

From source file:Main.java

/**
 * Get first value (value of first child) from the specified Element.<br>
 * If no value found 'def' value is returned.
 *//*from  w ww. ja v a 2  s.co  m*/
public static String getFirstValue(Element element, String def) {
    if (element != null) {
        final Node child = element.getFirstChild();
        if (child != null)
            return child.getNodeValue();
    }

    return def;
}

From source file:Main.java

public static String getTextValue(Element ele, String tagName) {
    String textVal = null;/*  w  w w.j ava  2  s. co  m*/
    NodeList nl = ele.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() > 0) {
        Element el = (Element) nl.item(0);
        if (el == null)
            return null;
        if (el.getFirstChild() == null)
            return null;
        textVal = el.getFirstChild().getNodeValue();
    }

    return textVal;
}

From source file:Main.java

public static List<Element> getElementChildsByTagName(Element elem, String TagName) {
    List<Element> elemList = new ArrayList<Element>();
    for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child instanceof Element && child.getNodeName().equals(TagName)) {
            elemList.add((Element) child);
        }/*  www .j a v  a2 s  .  c  o  m*/
    }
    return elemList;
}

From source file:Main.java

public static Element getDirectChildElement(Element p_rootElement, String p_elementName) {
    for (Node node = p_rootElement.getFirstChild(); node != null; node = node.getNextSibling()) {
        if (node.getNodeName().equalsIgnoreCase(p_elementName))
            return (Element) node;
    }/*from w ww .ja  va  2 s.  co  m*/
    return null;
}

From source file:Main.java

public static Element first(Element root, String localName) {
    if (root == null)
        return null;
    for (Node n1 = root.getFirstChild(); n1 != null; n1 = n1.getNextSibling()) {
        if (n1.getNodeType() != Node.ELEMENT_NODE)
            continue;
        return Element.class.cast(n1);
    }/*from   ww  w. jav  a2  s . c  o  m*/
    return null;
}