Android XML Node Value Get getText(Node node, String tag)

Here you can find the source of getText(Node node, String tag)

Description

get Text

Declaration

public static String getText(Node node, String tag) 

Method Source Code

//package com.java2s;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static String getText(Node node, String tag) {
        Node n = getChild(node, tag);

        if (n != null) {
            Node text = n.getFirstChild();

            if (text != null) {
                String s = text.getNodeValue();
                return s.trim();
            }/* www .  j  av a 2  s.  co  m*/
        }
        return "";
    }

    public static Node getChild(Node node, String tag) {
        if (node == null) {
            return null;
        }

        NodeList childNodes = node.getChildNodes();

        if (childNodes == null) {
            return null;
        }

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            if (item != null) {
                String name = item.getNodeName();
                if (tag.equalsIgnoreCase(name)) {
                    return item;
                }
            }
        }
        return null;
    }
}

Related

  1. getNextElement(Node el)
  2. getNodeValue(Node node)
  3. getText(Node n)
  4. getText(Node node, String tag)
  5. getText(Node node, String tag)
  6. getTextContent(Node n)
  7. getTextContent(Node n)
  8. getTextContent(Node n, StringBuffer buf)
  9. getTextContent(org.w3c.dom.Node element)