Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 Firestar Software, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Firestar Software, Inc. - initial API and implementation
 *
 * Author:
 *     Gabriel Oancea
 *
 *******************************************************************************/

import org.w3c.dom.*;

public class Main {
    /**
     * Get the text of a text (simple content) element. Will return an empty string if not found or empty.
     * 
     * @param e The element.
     * @return The text of the specified element, null if it has no text nodes.
     */
    public static String getTextString(Element e) {
        String s = getText(e);
        return s == null ? "" : s;
    }

    /**
     * Get the trimmed text of a text (simple content) element.
     * 
     * @param e The element.
     * @return The text of the specified element, null if it has no text nodes.
     */
    public static String getText(Element e) {
        if (e == null)
            return null;
        NodeList lst = e.getChildNodes();
        int size = lst.getLength();
        for (int i = 0; i < size; i++) {
            Node n = lst.item(i);
            if (n.getNodeType() == Node.TEXT_NODE) {
                Text t = (Text) n;
                String s = t.getData();
                return s == null ? null : s.trim();
            }
        }
        return null;
    }
}