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;

import java.util.ArrayList;
import java.util.Collection;

public class Main {
    public static String fetchSubElementValue(Element e, String subElementName) {
        Element element = fetchSubElement(e, subElementName);
        if (element != null) {
            return getTextContent(element);
        }
        return null;
    }

    public static String fetchSubElementValue(Element parent, String subElementName, String lang) {
        Collection<Element> elements = fetchSubElements(parent, subElementName);

        lang = lang.toUpperCase();

        for (Element e : elements) {
            String lng = e.getAttribute("xml:lang");
            if (lng != null && lang.equals(lng.toUpperCase())) {
                return getTextContent(e);
            }
        }

        return fetchSubElementValue(parent, subElementName);
    }

    public static Element fetchSubElement(Element e, String subElementName) {
        if (e == null || subElementName == null) {
            return null;
        }

        subElementName = subElementName.toUpperCase();

        NodeList list = e.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i) instanceof Element) {
                Element element = (Element) list.item(i);
                if (element.getTagName().toUpperCase().equals(subElementName)) {
                    return element;
                }
            }
        }

        return null;
    }

    public static String getTextContent(Node e) {
        if (e == null || e.getNodeType() != Node.ELEMENT_NODE) {
            return null;
        }

        NodeList nodes = e.getChildNodes();
        StringBuilder text = new StringBuilder();

        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = e.getFirstChild();

            if (node != null && node.getNodeType() == Node.TEXT_NODE) {
                String s = node.getNodeValue();
                if (s != null) {
                    text.append(s);
                }
            }
        }

        if (text.length() > 0) {
            return text.toString();
        } else {
            return null;
        }
    }

    public static Collection<Element> fetchSubElements(Element e, String subElementName) {
        if (e == null || subElementName == null) {
            return null;
        }

        subElementName = subElementName.toUpperCase();

        Collection<Element> elements = new ArrayList<Element>();

        NodeList list = e.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            if (list.item(i) instanceof Element) {
                Element element = (Element) list.item(i);
                if (element.getTagName().toUpperCase().equals(subElementName)) {
                    elements.add(element);
                }
            }
        }

        return elements;
    }
}