Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import org.w3c.dom.Element;

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

public class Main {
    public static String getFirstChildTextByTagName(Element element, String tagName) {
        Element e = getFirstChildElementByTagName(element, tagName);
        return e == null ? null : e.getTextContent().trim();
    }

    /**
     * Gets the first element with the given tag name in the immediate child elements.
     * 
     * @param element
     * @param tagName
     * @return
     */
    public static Element getFirstChildElementByTagName(Node element, String tagName) {
        NodeList list = element.getChildNodes();
        int size = list.getLength();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                Node node = list.item(i);
                if (node instanceof Element) {
                    Element e = (Element) node;
                    if (e.getTagName().equals(tagName)) {
                        return e;
                    }
                }
            }
        }
        return null;
    }
}