Java XML Parse File parseXmlFile(String xmlFile)

Here you can find the source of parseXmlFile(String xmlFile)

Description

Attempts to parse out a valid xml Document from the passed string.

License

Open Source License

Parameter

Parameter Description
xmlFile a parameter

Exception

Parameter Description
IOException an exception
ParserConfigurationException an exception
SAXException an exception

Declaration

public static Document parseXmlFile(String xmlFile)
        throws IOException, ParserConfigurationException, SAXException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;

import java.io.File;
import java.io.FileReader;

import java.io.IOException;

import java.io.StringReader;

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

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    /**// ww  w.j  ava2  s  .c o m
     * Attempts to parse out a valid xml <code>Document</code> from the passed string.
     * All newline, tabs and carrage return characters will be removed.
     * @param xmlFile
     * @return
     * @throws IOException
     * @throws ParserConfigurationException
     * @throws SAXException
     */
    public static Document parseXmlFile(String xmlFile)
            throws IOException, ParserConfigurationException, SAXException {

        String regex1 = "[\n\t\r]";

        String stringOut = readFileAsString(xmlFile);

        stringOut = stringOut.replaceAll(regex1, "");
        // System.out.println(stringOut + "!!!");
        //parse using builder to get DOM representation of the XML file

        return parseXmlString(stringOut);
    }

    public static Document parseXmlFile(File xmlFile) {
        Document out = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            String regex = "[\n\t]";
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            String stringOut = readFileAsString(xmlFile.getPath());

            stringOut.replaceAll(regex, "");

            // System.out.println(stringOut);
            //parse using builder to get DOM representation of the XML file
            out = db.parse(stringOut);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        } catch (SAXException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return out;
    }

    public static String readFileAsString(String filePath) throws java.io.IOException {

        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new FileReader(filePath));

        char[] buf = new char[1024];
        int numRead = 0;

        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(buf, 0, numRead);
        }
        reader.close();
        return fileData.toString();
    }

    public static Document parseXmlString(String xmlString)
            throws ParserConfigurationException, SAXException, IOException {
        //get the factory

        Document out = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        //Using factory get an instance of document builder
        DocumentBuilder db = dbf.newDocumentBuilder();
        // Create InputStream source for parser.
        InputSource inputSource = new InputSource(new StringReader(xmlString));
        //parse using builder to get DOM representation of the XML file

        out = db.parse(inputSource);

        return out;
    }
}

Related

  1. parseXmlFile(File file, boolean validating)
  2. parseXMLFile(final File xmlFile)
  3. parseXmlFile(String filename, boolean validating)
  4. parseXmlFile(String in)
  5. parseXmlFile(String xml)
  6. parseXmlFile(String xmlFile)