Here you can find the source of getAttribute(File file, String attr)
public static String getAttribute(File file, String attr)
//package com.java2s; //License from project: Open Source License import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; public class Main { public static String nameTag = "name"; public static String sectionTag = "section"; public static String getAttribute(File file, String attr) { Logger logger = Logger.getLogger("eu.excitementproject.eop.util.runner.ConfigFileUtils:getAttribute"); logger.info("Looking for a value for attribute: " + attr); Document configDoc = parse(file); NodeList sectionList = configDoc.getElementsByTagName(sectionTag); String value = findAttributeRec(sectionList, attr); if (value != null) { logger.info("Value for attribute " + attr + " : " + value); }/*from www. j a v a 2 s . c om*/ return value; } public static String getAttribute(File file, String configTag, String edaTag, String attr) { Document configDoc = parse(file); NodeList sectionList = configDoc.getElementsByTagName(sectionTag); Logger logger = Logger.getLogger("eu.excitementproject.eop.util.runner.ConfigFileUtils:getAttribute"); NodeList nodesList = findNodesWithTag(sectionList, configTag); String edaName = findAttribute(nodesList, edaTag); logger.info("EDA class name from config file: " + edaName); return edaName; } public static Document parse(File file) { Document configDoc = null; try { File fXmlFile = file; DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); configDoc = dBuilder.parse(fXmlFile); configDoc.getDocumentElement().normalize(); } catch (Exception e) { e.printStackTrace(); } return configDoc; } private static String findAttributeRec(NodeList list, String attr) { String val = null; int i = 0; boolean found = false; while (i < list.getLength() && !found) { Node node = list.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.hasAttribute(nameTag) && element.getAttribute(nameTag).equals(attr)) { found = true; val = element.getTextContent(); } } if (!found && node.hasChildNodes()) { val = findAttributeRec(node.getChildNodes(), attr); if (val != null) { found = true; } } i++; } return val; } private static NodeList findNodesWithTag(NodeList list, String tag) { NodeList foundNodes = null; int i = 0; boolean found = false; while (i < list.getLength() && !found) { Node node = list.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.hasAttribute(nameTag) && element.getAttribute(nameTag).equals(tag)) { found = true; foundNodes = element.getChildNodes(); } } i++; } return foundNodes; } private static String findAttribute(NodeList list, String attr) { String val = null; int i = 0; boolean found = false; while (i < list.getLength() && !found) { Node node = list.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; if (element.hasAttribute(nameTag) && element.getAttribute(nameTag).equals(attr)) { found = true; val = element.getTextContent(); } } i++; } return val; } }