Java XML Element Get Value getTrimmedText(Element element)

Here you can find the source of getTrimmedText(Element element)

Description

Get the trimmed text for an element, which is considered the first Text child element.

License

Open Source License

Parameter

Parameter Description
element a parameter

Declaration

public static String getTrimmedText(Element element) 

Method Source Code

//package com.java2s;
/*//  ww w  . ja  v  a 2 s .  co m
  XMLUtils.java
    
  (c) 2010-2011 Edward Swartz
    
  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
 */

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    /**
     * Get the trimmed text for an element, which is considered the first Text child element.
     * @param element
     * @returns the text or <code>""</code>
     */
    public static String getTrimmedText(Element element) {
        String text = getText(element);
        if (text != null)
            return text.trim();
        return null;
    }

    /**
     * Get the text for an element, which is considered the first Text child element.
     * @param element
     * @returns the text or <code>null</code>
     */
    public static String getText(Element element) {
        NodeList nodeList = element.getChildNodes();
        if (nodeList.item(0) instanceof Text) {
            return nodeList.item(0).getTextContent();
        }
        return null;
    }
}

Related

  1. getTextValue(Element valueEle)
  2. getTextValue(Element valueEle)
  3. getTextValue(final Element elem)
  4. getTextValue(final Element element, final String tagName)
  5. getTextWithoutTrim(Element elem)
  6. getTrimmedTextContent(Element element)
  7. getTrimmedTextContent(Element element)
  8. getTrimmedTextValue(Element valueEle)
  9. getType(Element elementType)