Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static String getElementTextByTagName(Element n, String elementName) {
        Element subEl = getElementByTagName(n, elementName);
        if (subEl != null)
            return getElementText(subEl);
        return null;
    }

    /**
     * Gets the first child Element with a given name
     * 
     * @param n
     *            the node get the children from
     * @param elementName
     *            the name of the child elements
     * @return the first child Element with a given name
     */
    public static Element getElementByTagName(Element n, String elementName) {
        NodeList subNodes = n.getElementsByTagName(elementName);
        int sz = subNodes.getLength();
        if (sz > 0)
            return (Element) subNodes.item(0);
        return null;
    }

    public static String getElementText(Element n) {
        NodeList childNodes = n.getChildNodes();
        String result = new String();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (node.getNodeType() == Node.TEXT_NODE) {
                result += node.getNodeValue();
            }
        }
        return result;
    }
}