get Value from XML Node - Java XML

Java examples for XML:XML Node

Description

get Value from XML Node

Demo Code


//package com.java2s;

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

public class Main {
    public static String getValue(Node node, String name) {
        Node n = getSubElement(node, name);
        if (n == null) {
            return "";
        }/* www .j  ava  2s.  c  om*/
        return n.getTextContent();
    }

    public static String getValue(Node node, String name, String subName) {
        Node n = getSubElement(node, name);
        if (n == null) {
            return "";
        }
        return getValue(n, subName);
    }

    private static Node getSubElement(Node parent, String name) {
        NodeList nodes = ((Element) parent).getElementsByTagName(name);
        if (nodes.getLength() == 0) {
            return null;
        }
        return nodes.item(0);
    }
}

Related Tutorials