Java XML Element Get Value getText(Element el)

Here you can find the source of getText(Element el)

Description

get the text string in an element (eg interspersed between child elements), or "" if there is none or if the Element is null.

License

Open Source License

Declaration

public static String getText(Element el) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.StringTokenizer;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    /**//ww w  .  j  a v a  2 s  .c  o  m
     * get the  text string in an element (eg interspersed between child elements), 
     * or "" if there is none or if the Element is null.
     * Tries to ignore white space text; but does not succeed.
     */
    public static String getText(Element el) {
        String res = "";
        if (el != null)
            try {
                el.normalize(); // does not help recognise white space
                NodeList nodes = el.getChildNodes();
                for (int i = 0; i < nodes.getLength(); i++)
                    if (nodes.item(i) instanceof Text) {
                        Text text = (Text) nodes.item(i);
                        // this filter seems to make no difference
                        if (!text.isElementContentWhitespace()) {
                            String tData = text.getData();
                            // this seems to be an effective way to catch pure white space
                            StringTokenizer nonWhiteSpace = new StringTokenizer(tData, "\n \t");
                            if (nonWhiteSpace.countTokens() > 0)
                                res = res + tData;
                        }
                    }
            } catch (Exception e) {
                System.out.println("Text failure: " + e.getMessage());
            }
        return res;
    }
}

Related

  1. getTagValue(Element element, String tagName)
  2. getText(Element aElement)
  3. getText(Element config)
  4. getText(Element config)
  5. getText(Element e)
  6. getText(Element elem)
  7. getText(Element elem)
  8. getText(Element elem)
  9. getText(Element elem)