Java XML Element to String getString(final Element element)

Here you can find the source of getString(final Element element)

Description

Get string value of an element.

License

Open Source License

Parameter

Parameter Description
element Element

Return

String of the node. Empty string if nothing found.

Declaration

public static String getString(final Element element) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015-2016 Oak Ridge National Laboratory.
 * 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.Node;

public class Main {
    /** Get string value of an element.
     *  @param element Element/*  ww  w. j a  va2s . c  o  m*/
     *  @return String of the node. Empty string if nothing found.
     */
    public static String getString(final Element element) {
        final Node text = element.getFirstChild();
        if (text == null) // <empty /> node
            return "";
        if ((text.getNodeType() == Node.TEXT_NODE || text.getNodeType() == Node.CDATA_SECTION_NODE))
            return text.getNodeValue();
        return "";
    }
}

Related

  1. getString(Element element)
  2. getString(Element element, String expr)
  3. getString(Element element, String name, String def)
  4. getString(Element root, String name)
  5. getString(Element xmlElement)
  6. getStringByTagName(Element element, String tag)
  7. getStringFromParagraphElement(Element element)
  8. getStringOptionsList(final Element configuration, final String optionPrefix, final String option)
  9. getStringProperty(Element properties, String name)