Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.w3c.dom.*;

public class Main {
    private static org.w3c.dom.Document doc;

    /**
     * Method t o return a single first Element with a specific attribute value. 
     * (maybe you can find a better method))
     * @param tagName string of the name tag xml.
     * @param nameAttribute string of the name attribute xml.
     * @return the element xml with the specific attribute.
     */
    public static Element selectFirstElementByAttribute(String tagName, String nameAttribute) {
        Element el = doc.getDocumentElement(); //get root element
        NodeList elementList = el.getElementsByTagName(tagName);
        for (int i = 0; i < elementList.getLength(); i++) {
            if (getAttribute((Element) elementList.item(i), nameAttribute) != null) {
                el = (Element) elementList.item(i);
                break;
            }
        }
        return el;
    }

    /**
     * Get an Attribute from an Element.  Returns an empty String if none found
     * http://www.java2s.com/Code/Java/XML/ReturnalistofnamedElementswithaspecificattributevalue.htm
     * @param element the containing Element.
     * @param name the attribute name.
     * @return Attribute as a String.
     */
    public static String getAttribute(Element element, String name) {
        return element.getAttribute(name);
    }
}