Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import org.w3c.dom.*;

public class Main {
    /**
     * returns the text value associated with the element
     *
     * @param target - the element
     * @return - the text value
     */
    public static String getElementValue(final Element target) {

        final NodeList nodeList = target.getChildNodes();
        if (nodeList == null) {
            return null;
        }
        for (int current = 0; current < nodeList.getLength(); current++) {
            final Node node = nodeList.item(current);
            if (node instanceof Text) {
                final Text text = (Text) node;
                final String value = text.getNodeValue();
                if ((value != null) && (value.length() > 0)) {
                    return value;
                }
            }
        }
        return "";
    }
}