get XML Element Value - Java XML

Java examples for XML:XML Element Value

Description

get XML Element Value

Demo Code


//package com.java2s;
import org.w3c.dom.Element;

public class Main {
    public static String getElementValue(Element parentElement, String tag) {
        if (parentElement.getElementsByTagName(tag).item(0) == null)
            return null;
        else if (parentElement.getElementsByTagName(tag).item(0)
                .getFirstChild() == null)
            return null;
        else/*from   www  .  j av a2  s . c  om*/
            return parentElement.getElementsByTagName(tag).item(0)
                    .getFirstChild().getNodeValue();
        //return (parentElement.getElementsByTagName(tag).item(0) == null
        //     ? "None"
        //     : parentElement.getElementsByTagName(tag).item(0).getFirstChild().getNodeValue()
        //      );
    }
}

Related Tutorials