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 getElementCDATAByTagName(Element current, String name) {

        return getElementCDATAByTagName(current, name, null);
    }

    public static String getElementCDATAByTagName(Element current, String name, String namespace) {

        Element e = getElementByTagName(current, name, namespace);

        if (e == null)
            return null;

        Node child = e.getFirstChild();

        if (child == null)
            return null;

        return child.getNodeValue();

    }

    public static Element getElementByTagName(Element current, String name) {
        return getElementByTagName(current, name, null);
    }

    public static Element getElementByTagName(Element current, String name, String namespace) {

        NodeList elements = null;

        if (namespace == null)
            elements = current.getElementsByTagName(name);
        else
            elements = current.getElementsByTagNameNS(namespace, name);

        if (elements == null)
            return null;

        if (elements.getLength() != 1)
            return null;

        return (Element) elements.item(0);

    }
}