Java XML Attribute Get getAttribute(File file, String attr)

Here you can find the source of getAttribute(File file, String attr)

Description

get Attribute

License

Open Source License

Declaration

public static String getAttribute(File file, String attr) 

Method Source Code


//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;
    }
}

Related

  1. getAttribute(Element element, String namespace, String name)
  2. getAttribute(Element element, String nodeName)
  3. getAttribute(Element ownerElem, String attrName)
  4. getAttribute(Element parent, String localName)
  5. getAttribute(Element parent, String localName, String namespaceURI)
  6. getAttribute(final List elements, final int index)
  7. getAttribute(final Node iNode, final String iAttributeName)
  8. getAttribute(final Node n, final String attrName, final String defaultValue)
  9. getAttribute(final Node node, final String attribname, final int def)