Here you can find the source of readXmlFile(String fileName)
Parameter | Description |
---|---|
fileName | a parameter |
Parameter | Description |
---|---|
Exception | an exception |
public static Document readXmlFile(String fileName) throws Exception
//package com.java2s; /******************************************************************************* * Copyright (c) 2015 CA. All rights reserved. * * This source file is licensed under the terms of the Eclipse Public License 1.0 * For the full text of the EPL please see https://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ 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; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.StringReader; public class Main { /**/*from w w w. j a v a 2s . c o m*/ * Parse the content of the given file as an XML document and return a new DOM * * @param fileName * @return xml Document * @throws Exception */ public static Document readXmlFile(String fileName) throws Exception { Document doc = null; try { File xmlFile = new File(fileName); DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); doc = builder.parse(xmlFile); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } catch (Exception e) { throw new Exception(e.getMessage(), e); } return doc; } /** * Reads an xml file into XML Document. * @param fileName * @return xml Document * @throws Exception */ public static Document readXmlFile(InputStream is) throws Exception { Document doc = null; String xmlStr = getStringFromInputStream(is); if (false == xmlStr.isEmpty()) doc = readXmlString(xmlStr); return doc; } /** * Returns the string of InputStream * * convert InputStream to String * @param is InputStream * @return */ private static String getStringFromInputStream(InputStream is) { BufferedReader br = null; StringBuilder sb = new StringBuilder(); String line; try { br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } /** * Reads an xml string into XML Document. * * @param xmlStr String containing xml * @return xml Document * @throws Exception */ public static Document readXmlString(String xmlStr) throws Exception { Document doc = null; try { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); domFactory.setNamespaceAware(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); InputSource inStream = new InputSource(); inStream.setCharacterStream(new StringReader(xmlStr)); doc = builder.parse(inStream); } catch (ParserConfigurationException e) { throw new Exception(e.getMessage(), e); } catch (SAXException e) { throw new Exception(e.getMessage(), e); } catch (IOException e) { throw new Exception(e.getMessage(), e); } catch (Exception e) { throw new Exception(e.getMessage(), e); } return doc; } }