Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//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 {
    /**
     * 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;
    }
}