Java XML Element Get Value getTextString(Element e)

Here you can find the source of getTextString(Element e)

Description

Get the text of a text (simple content) element.

License

Open Source License

Parameter

Parameter Description
e The element.

Return

The text of the specified element, null if it has no text nodes.

Declaration

public static String getTextString(Element e) 

Method Source Code

//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:/*  ww w  . j a v  a2s.c o m*/
 *     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;
    }
}

Related

  1. getTextFromFirstSubEleByName(Element element, String tagName)
  2. getTextFromTag(Element current, String tag)
  3. getTextList(Element elem, String name)
  4. getTextOfElement(Element e)
  5. getTexts(Element root, String elementName)
  6. getTextTrim(Element element)
  7. getTextTrim(Element element)
  8. getTextValue(Element el, String tagName)
  9. getTextValue(Element ele, String tagName)