Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import java.io.*;
import java.net.URL;

import static javax.xml.xpath.XPathConstants.NODE;

public class Main {
    public static String getAttribute(String fileName, String xPathExpression, String attributeName) {
        try {
            Document document = parseXML(new File(fileName));
            XPath xpath = XPathFactory.newInstance().newXPath();
            XPathExpression xpathExpression = xpath.compile(xPathExpression);
            Node node = (Node) xpathExpression.evaluate(document, NODE);
            return getAttributeValueByName(node, attributeName);
        } catch (Exception e) {
            throw new RuntimeException("Failed to extract element from:" + fileName, e);
        }
    }

    /**
     * Parses an input source and generates an XML document.
     * @param is An input source to an XML source.
     * @return An XML doucument.
     */
    public static Document parseXML(InputSource is) {
        try {
            Document doc = null;
            DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            doc = documentBuilder.parse(is);
            return doc;
        } catch (Exception e) {
            throw new RuntimeException("Failed to parse XML source", e);
        }
    }

    /**
     * Parses an input stream and generates an XML document.
     * @param is An input stream to an XML source.
     * @return An XML doucument.
     */
    public static Document parseXML(InputStream is) {
        return parseXML(new InputSource(is));
    }

    /**
     * Parses a file and generates an XML document.
     * @param file
     * @return An XML doucument.
     */
    public static Document parseXML(File file) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            return parseXML(fis);
        } catch (Exception e) {
            throw new RuntimeException("Failed to open XML file:" + file, e);
        } finally {
            try {
                fis.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Parses the input stream of a URL and generates an XML document.
     * @param xmlUrl The URL of the XML document.
     * @return The parsed document.
     */
    public static Document parseXML(URL xmlUrl) {
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = xmlUrl.openConnection().getInputStream();
            bis = new BufferedInputStream(is);
            return parseXML(bis);
        } catch (Exception e) {
            throw new RuntimeException("Failed to read XML URL:" + xmlUrl, e);
        } finally {
            try {
                bis.close();
            } catch (Exception e) {
            }
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * Parses an XML string and generates an XML document.
     * @param xml The XML to parse
     * @return An XML doucument.
     */
    public static Document parseXML(CharSequence xml) {
        StringReader sr = new StringReader(xml.toString());
        return parseXML(new InputSource(sr));
    }

    /**
     * Searches throgh the passed NamedNodeMap for an attribute and returns it if it is found.
     * If it is not found, returns a null.
     * @param nnm NamedNodeMap
     * @param name String
     * @return String
     */
    public static String getAttributeValueByName(NamedNodeMap nnm, String name) {
        for (int i = 0; i < nnm.getLength(); i++) {
            Attr attr = (Attr) nnm.item(i);
            if (attr.getName().equalsIgnoreCase(name)) {
                return attr.getValue();
            }
        }
        return null;
    }

    /**
     * Returns the attribute value for the passed name in the passed node.
     * @param node the node to get the attribute from
     * @param name the name of the attribute
     * @return The attribute value or null if it is not found.
     */
    public static String getAttributeValueByName(Node node, String name) {
        return getAttributeValueByName(node.getAttributes(), name);
    }
}