Java XML Element Get Value getTextValue(Element element)

Here you can find the source of getTextValue(Element element)

Description

Returns the text value of an element (title, alt or contents).

License

Apache License

Parameter

Parameter Description
element The element.

Return

The text value of the element.

Declaration

public static String getTextValue(Element element) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Element;

public class Main {
    private static final int TEXT_CUTOFF = 50;

    /**/* ww  w .java2  s  . c  om*/
     * Returns the text value of an element (title, alt or contents). Note that the result is 50
     * characters or less in length.
     * 
     * @param element
     *            The element.
     * @return The text value of the element.
     */
    public static String getTextValue(Element element) {
        String ret = "";
        String textContent = element.getTextContent();
        if (textContent != null && !textContent.equals("")) {
            ret = textContent;
        } else if (element.hasAttribute("title")) {
            ret = element.getAttribute("title");
        } else if (element.hasAttribute("alt")) {
            ret = element.getAttribute("alt");
        }
        if (ret.length() > TEXT_CUTOFF) {
            return ret.substring(0, TEXT_CUTOFF);
        } else {
            return ret;
        }
    }
}

Related

  1. getTextValue(Element ele, String tagName)
  2. getTextValue(Element ele, String tagName)
  3. getTextValue(Element ele, String tagName)
  4. getTextValue(Element ele, String tagName)
  5. getTextValue(Element ele, String tagName)
  6. getTextValue(Element element)
  7. getTextValue(Element element, String name)
  8. getTextValue(Element element, String tagName)
  9. getTextValue(Element element, String tagName)