Example usage for org.w3c.dom Element getTextContent

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

Introduction

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

Prototype

public String getTextContent() throws DOMException;

Source Link

Document

This attribute returns the text content of this node and its descendants.

Usage

From source file:Main.java

public static String GetFirstInstance(NodeList nList) {
    for (int i = 0; i < nList.getLength(); i++) {
        Node nNode = nList.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            if (eElement != null)
                return eElement.getTextContent();
        } // end if nnode.getnodetype()
    } //end for int temp
    return new String("");
}

From source file:Main.java

public static String getChildElementValue(Element parent, String tagName) {
    Element element = getChildElement(parent, tagName);
    return element != null ? element.getTextContent() : null;
}

From source file:Main.java

public static String getFirstChildTextByTagName(Element element, String tagName) {
    Element e = getFirstChildElementByTagName(element, tagName);
    return e == null ? null : e.getTextContent().trim();
}

From source file:Main.java

public static String getXpathValue(String xpathStr, Document doc) throws Exception {
    String value = null;/*w  w w .  ja v a2s .c om*/
    try {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Element e = (Element) xpath.evaluate(xpathStr, doc, XPathConstants.NODE);
        value = e.getTextContent();
    } catch (Exception e) {
        throw e;
    }
    return value;
}

From source file:Main.java

/**
 * Gets the child element content./*from   ww  w . j  a  va 2s  .  co m*/
 * 
 * @param el 
 * @param tagName 
 * 
 * @return the child element content
 */
public static String getChildElementContent(Element el, String tagName) {
    Element el2 = getChildElement(el, tagName);
    return el2 == null ? "" : el2.getTextContent();
}

From source file:Main.java

/**
 * @param parent the parent XML element//  w  ww  . j av  a2s .  c  om
 * @param childName the child node name
 * @return the child XML element text with specified node name or <code>null</code> if does not exist
 */
public static String getChildElementText(final Element parent, final String childName) {

    Element child = getChildElement(parent, childName);
    return child == null ? null : child.getTextContent();
}

From source file:Main.java

/**
 * Returns value of a child element from a given XML element
 * //from  ww  w  . j a  va  2s  . c  om
 * @param element
 *            A Elemnt
 * @param tagName
 *            The name of the child elment
 * @return The value of the child element
 */
public static String getChildElementContent(Element element, String tagName) {
    Element el2 = getChildElement(element, tagName);
    return el2 == null ? "" : el2.getTextContent();
}

From source file:Main.java

public static String getElementContents(Element element, String elementName) throws IOException {
    Element first = getFirstElement(element, elementName);
    if (first != null) {
        return first.getTextContent().trim();
    } else {//from  ww w. j ava 2s  .  c o m
        return null;
    }
}

From source file:Main.java

public static String getElementText(Element namedElement) {
    if (namedElement == null) {
        return null;
    }/*ww  w .  j  av a2s .  c  o  m*/
    String text = namedElement.getTextContent();
    return (text.isEmpty()) ? null : text;
}

From source file:Main.java

/**
 * @param parent the parent XML element/*from w w  w.j  a  v  a  2s .  c  om*/
 * @param childName the child node name
 * @param defaultText the default text if child element does not exist
 * @return the child XML element text with specified node name or default value if does not exist
 */
public static String getChildElementText(final Element parent, final String childName,
        final String defaultText) {

    Element child = getChildElement(parent, childName);
    return child == null ? defaultText : child.getTextContent();
}