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;

public class Main {
    public static String getTagPropertyValue(Element root, String tagName, String attr) {
        Element interestedNodes = getChildElement(root, tagName);

        String desc = interestedNodes.getAttribute(attr);

        return desc;
    }

    /**
     * Access an immediate child element inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return the first immediate child element inside element, or
     * <code>null</code> if the child element is not found.
     */
    public static Element getChildElement(Element element, String elemName) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getNodeName())) {
                    return (Element) n;
                }
            }
        }
        return null;
    }

    /**
     * Access an immediate child element inside the given Element
     *
     * @param element the starting element, cannot be null.
     * @param namespaceURI name space of the child element to look for,
     * cannot be null. Use "*" to match all namespaces.
     * @param elemName the name of the child element to look for, cannot be
     * null.
     * @return the first immediate child element inside element, or
     * <code>null</code> if the child element is not found.
     */
    public static Element getChildElement(Element element, String namespaceURI, String elemName) {
        NodeList list = element.getChildNodes();
        int len = list.getLength();

        for (int i = 0; i < len; i++) {
            Node n = list.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                if (elemName.equals(n.getLocalName())
                        && ("*".equals(namespaceURI) || namespaceURI.equals(n.getNamespaceURI()))) {
                    return (Element) n;
                }
            }
        }
        return null;
    }
}