Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

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

public class Main {

    public static String getElementValue(Element parent, String tagName) {
        String value = null;

        Element element = getChildElement(parent, tagName);
        if (element != null) {
            value = element.getTextContent();
        }

        return value;
    }

    public static Element getChildElement(Element parent, String tagName) {
        List<Element> children = getChildElements(parent, tagName);
        if (children.isEmpty()) {
            return null;
        } else {
            return children.get(0);
        }
    }

    public static List<Element> getChildElements(Element parent, String tagName) {
        NodeList nodes = parent.getElementsByTagName(tagName);
        List<Element> elements = new ArrayList<Element>();
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node instanceof Element && node.getParentNode() == parent) {
                elements.add((Element) node);
            }
        }
        return elements;
    }
}