Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * Get string value for a tag
     * 
     * @param xmlElement
     *            Root Element of subtree in which required tag is to be
     *            searched
     * @param key
     *            Required tage name
     * @return String value
     */
    public static String getStringValueForXMLTagFromAnywhere(Element xmlElement, String key) {

        // Start - testing
        String value = xmlElement.getAttribute(key);
        System.out.println("Value is - " + value);
        // End - testing

        NodeList nl = xmlElement.getElementsByTagName(key);
        String output = "nothing";
        if (nl.getLength() > 0) {
            Node node = nl.item(0).getFirstChild();
            if (node != null) {
                output = node.getNodeValue().trim();
                return output;
            }
        }
        return output;
    }
}