Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static String getFirstValue(Element elem, String name) {
        String retval = getSimpleElementText(getFirstNode(elem, name));
        return retval;
    }

    public static String getSimpleElementText(Element node) {
        if (node == null)
            return null;
        StringBuffer sb = new StringBuffer();
        NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child instanceof Text) {
                sb.append(child.getNodeValue());
            }
        }
        return sb.toString();
    }

    public static Element getFirstNode(Element elem, String name) {
        NodeList nl = elem.getElementsByTagName(name);
        if (nl.getLength() > 0)
            return (Element) nl.item(0);
        return null;
    }
}