Java XML Node Text Value getTextContent(Node node, String defaultValue)

Here you can find the source of getTextContent(Node node, String defaultValue)

Description

Returns the text content of a node or an empty string if an error occurs.

License

Creative Commons License

Parameter

Parameter Description
node The node.
defaultValue The default value.

Return

The text content or the default value.

Declaration

public static String getTextContent(Node node, String defaultValue) 

Method Source Code


//package com.java2s;
//License from project: Creative Commons License 

import org.w3c.dom.Node;

public class Main {
    /**/*from  w w  w  . jav  a 2 s  . c o  m*/
     * Returns the text content of a node or an empty string if an error occurs.
     * 
     * @param node The node.
     * @return The text content or an empty string.
     */
    public static String getTextContent(Node node) {
        return getTextContent(node, "");
    }

    /**
     * Returns the text content of a node or an empty string if an error occurs.
     * 
     * @param node The node.
     * @param defaultValue The default value.
     * @return The text content or the default value.
     */
    public static String getTextContent(Node node, String defaultValue) {
        try {
            String content = null;
            if (node != null) {
                content = node.getTextContent();
            }
            return content != null ? content : defaultValue;
        } catch (Exception e) {
            return defaultValue;
        }
    }
}

Related

  1. getTextContent(Node node)
  2. getTextContent(Node node)
  3. getTextContent(Node node)
  4. getTextContent(Node node)
  5. getTextContent(Node node)
  6. getTextContent(Node node, String path)
  7. getTextContent(org.w3c.dom.Node element)
  8. getTextContentOld(final Node node)
  9. getTextContents(Node node)