Java tutorial
//package com.java2s; /** * Copyright (C) 2013-2016 Laurent GUERIN - NanoJ project org. ( http://www.nanoj.org/ ) * * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.gnu.org/licenses/lgpl.html * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; public class Main { private static DocumentBuilder getDocumentBuilder() throws Exception { DocumentBuilderFactory factory = null; DocumentBuilder builder = null; //--- Create a new factory instance via JAXP try { factory = DocumentBuilderFactory.newInstance(); } catch (FactoryConfigurationError e) { throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilderFactory ", e); } catch (Throwable t) { throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilderFactory ", t); } if (factory != null) { //--- Create a DOM parser ( instance of a builder ) try { builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilder ", e); } catch (Throwable t) { throw new Exception("XmlUtil.parse() : Cannot create DocumentBuilder ", t); } } else { throw new Exception("XmlUtil.parse() : DocumentBuilderFactory instance is null"); } if (builder == null) { throw new Exception("XmlUtil.parse() : DocumentBuilder instance is null"); } return builder; } }