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 {
    public static String getSubTagValue(Element root, String tagName, String subTagName) {
        String returnString = "";
        NodeList list = root.getElementsByTagName(tagName);
        for (int loop = 0; loop < list.getLength(); loop++) {
            Node node = list.item(loop);
            if (node != null) {
                NodeList children = node.getChildNodes();
                for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                    Node child = children.item(innerLoop);
                    if ((child != null) && (child.getNodeName() != null)
                            && (child.getNodeName().equals(subTagName))) {
                        Node grandChild = child.getFirstChild();
                        if (grandChild.getNodeValue() != null) {
                            return grandChild.getNodeValue();
                        }
                    }
                }
            }
        }
        return returnString;
    }

    private static String getSubTagValue(Node node, String subTagName) {
        String returnString = "";
        if (node != null) {
            NodeList children = node.getChildNodes();
            for (int innerLoop = 0; innerLoop < children.getLength(); innerLoop++) {
                Node child = children.item(innerLoop);
                if ((child != null) && (child.getNodeName() != null) && (child.getNodeName().equals(subTagName))) {
                    Node grandChild = child.getFirstChild();
                    if (grandChild.getNodeValue() != null) {
                        return grandChild.getNodeValue();
                    }
                }
            }
        }
        return returnString;
    }
}