Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*---------------------------------------------------------------
 *  Copyright 2005 by the Radiological Society of North America
 *
 *  This source software is released under the terms of the
 *  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
 *----------------------------------------------------------------*/

import java.io.*;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class Main {
    /**
     * Parses an XML file and returns the name of the document element.
     * @param realFilePath the path to the file containing the XML to parse.
     * @return the name of the document element, or null if the file does not parse.
     */
    public static String getDocumentElementName(String realFilePath) {
        return getDocumentElementName(new File(realFilePath));
    }

    /**
     * Parses an XML file and returns the name of the document element.
     * @param file the file containing the XML to parse.
     * @return the name of the document element, or null if the file does not parse.
     */
    public static String getDocumentElementName(File file) {
        try {
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document d = db.parse(file);
            Element root = d.getDocumentElement();
            return root.getTagName();
        } catch (Exception e) {
            return null;
        }
    }
}