Example usage for org.w3c.dom Text isElementContentWhitespace

List of usage examples for org.w3c.dom Text isElementContentWhitespace

Introduction

In this page you can find the example usage for org.w3c.dom Text isElementContentWhitespace.

Prototype

public boolean isElementContentWhitespace();

Source Link

Document

Returns whether this text node contains <a href='http://www.w3.org/TR/2004/REC-xml-infoset-20040204#infoitem.character'> element content whitespace</a>, often abusively called "ignorable whitespace".

Usage

From source file:Main.java

/**
 * Get the content of the first Text child element of the passed element.
 *
 * @param aStartNode//  ww  w  .j a va 2  s. c  o m
 *        the element to scan for a TextNode child
 * @return <code>null</code> if the element contains no text node as child
 */
@Nullable
public static String getFirstChildText(@Nullable final Node aStartNode) {
    if (aStartNode != null) {
        final NodeList aNodeList = aStartNode.getChildNodes();
        final int nLen = aNodeList.getLength();
        for (int i = 0; i < nLen; ++i) {
            final Node aNode = aNodeList.item(i);
            if (aNode instanceof Text) {
                final Text aText = (Text) aNode;

                // ignore whitespace-only content
                if (!aText.isElementContentWhitespace())
                    return aText.getData();
            }
        }
    }
    return null;
}

From source file:Main.java

/**
 * 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.
 *//*from  ww  w  .  java 2  s.  c o m*/
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;
}