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 getText(Element element, String tagName) {
        return getText(element, tagName, null);
    }

    public static String getText(Element element, String tagName, String def) {
        Element child = getChild(element, tagName);
        String text = null;
        if (child != null)
            text = child.getTextContent();
        if (text == null)
            text = def;
        return text;
    }

    public static Element getChild(Element element, String tagName) {
        NodeList nodes = element.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node instanceof Element) {
                Element child = (Element) node;
                if (child.getTagName().equals(tagName))
                    return child;
            }
        }
        return null;
    }
}