Java XML Element Get Value getText(Element elem, String ifnull)

Here you can find the source of getText(Element elem, String ifnull)

Description

get Text

License

Apache License

Declaration

public static String getText(Element elem, String ifnull) 

Method Source Code

//package com.java2s;
/*/* w  ww  . j  ava 2s  .c  o m*/
 * Copyright (c) Jim Coles (jameskcoles@gmail.com) 2018 through present.
 *
 * Licensed under the following license agreement:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Also see the LICENSE file in the repository root directory.
 */

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

public class Main {
    public static String getText(Element elem) {
        return ((Text) elem.getFirstChild()).getData();
    }

    public static String getText(Element elem, String ifnull) {
        String retVal = null;
        Node node = elem.getFirstChild();
        if (node != null) {
            if (node.getNodeType() == Node.TEXT_NODE) {
                retVal = ((Text) node).getData();
            } else {
                System.out.println("Invalid element type in getText().");
            }
        } else {
            retVal = ifnull;
        }
        return retVal;
    }
}

Related

  1. getText(Element el)
  2. getText(Element elem)
  3. getText(Element elem)
  4. getText(Element elem)
  5. getText(Element elem)
  6. getText(Element elem, String name)
  7. getText(Element element)
  8. getText(Element element)
  9. getText(Element element)