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.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the value of the attribute with the given name of the first tag found with the given tag name in the given
     * document<br/>.
     *
     * @param doc
     * @param tagName
     * @param attributeName
     * @return the value of the attribute with the given name of the node or the empty string, if no node with this name
     * exits in this document or the attribute does not exist
     */
    public static String getTagAttributeValue(Document doc, String tagName, String attributeName) {
        NodeList tagList = doc.getElementsByTagName(tagName);
        if (tagList.getLength() > 0) {
            NamedNodeMap attributes = tagList.item(0).getAttributes();
            if (attributes != null) {
                Node attribute = attributes.getNamedItem(attributeName);
                if (attribute != null) {
                    return attribute.getNodeValue().trim();
                }
            }
        }
        return "";
    }
}