Java XML Parse File parse(String fileName)

Here you can find the source of parse(String fileName)

Description

Parse the XML file and create Document

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Return

Document

Declaration

public static Document parse(String fileName) throws Exception 

Method Source Code

//package com.java2s;
/************************************************************************
 MIT License// w  w  w.j  av  a 2  s  .  c  o m
    
 Copyright (c) 2010 University of Connecticut
    
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
    
 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.
    
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
***********************************************************************/

import java.io.File;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;

public class Main {
    /**
     * Parse the XML file and create Document
     * @param fileName
     * @return Document
     */
    public static Document parse(String fileName) throws Exception {
        Document document = null;
        // Initiate DocumentBuilderFactory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        // To get a validating parser
        factory.setValidating(false);
        // To get one that understands namespaces
        factory.setNamespaceAware(true);

        //try {
        // Get DocumentBuilder
        DocumentBuilder builder = factory.newDocumentBuilder();
        // Parse and load into memory the Document
        document = builder.parse(new File(fileName));
        return document;
        /*
        } catch (SAXParseException spe) {
        // Error generated by the parser
        System.out.println("\n** Parsing error , line " + spe.getLineNumber()
                 + ", uri " + spe.getSystemId());
        System.out.println(" " + spe.getMessage() );
        // Use the contained exception, if any
        Exception x = spe;
        if (spe.getException() != null)
          x = spe.getException();
        x.printStackTrace();
        } catch (SAXException sxe) {
        // Error generated during parsing
        Exception x = sxe;
        if (sxe.getException() != null)
          x = sxe.getException();
        x.printStackTrace();
        } catch (ParserConfigurationException pce) {
        // Parser with specified options can't be built
        pce.printStackTrace();
        } catch (IOException ioe) {
        // I/O error
        ioe.printStackTrace();
        }
            
        return null; //*/
    }
}

Related

  1. parse(String fileName)
  2. parse(String fileName)
  3. parse(String filename)
  4. parse(String filename)
  5. parse(String fileName)
  6. parse(String xmlFile)
  7. parseAIMLFile(File file)
  8. parseData(String filename)
  9. parseDoc(File xmlFile)