Example usage for javax.xml.parsers ParserConfigurationException getMessage

List of usage examples for javax.xml.parsers ParserConfigurationException getMessage

Introduction

In this page you can find the example usage for javax.xml.parsers ParserConfigurationException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:Main.java

/**
 * Creates a new empty DOM {@link Document}.
 *
 * @return the created {@code Document}.
 */// w  w  w.j a v a  2  s .c  o  m
public static Document createEmptyDomDocument() {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        return documentBuilder.newDocument();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Internal error: ParserConfigurationException: " + e.getMessage());
    }
}

From source file:Main.java

public static Document parseFile(String fileName) {
    System.out.println("Parsing XML file... " + fileName);

    DocumentBuilder docBuilder;/*from  w w w.  ja v  a  2s .  com*/
    Document doc = null;
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringElementContentWhitespace(true);
    try {
        docBuilder = docBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        System.out.println("Wrong parser configuration: " + e.getMessage());
        return null;
    }
    File sourceFile = new File(fileName);
    try {
        doc = docBuilder.parse(sourceFile);
    } catch (SAXException e) {
        System.out.println("Wrong XML file structure: " + e.getMessage());
        return null;
    } catch (IOException e) {
        System.out.println("Could not read source file: " + e.getMessage());
    }

    return doc;
}

From source file:Main.java

/**
 * Loads an xml file on disk into a document
 *
 * @param  xmlFileName - name of the file on disk, including path
 * @return Document    - loaded from the specified xml file on disk
 *///from   w ww  .jav  a 2 s .  com
public static Document loadXmlFromFile(String xmlFileName) {
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    docFactory.setNamespaceAware(true);

    try {
        return docFactory.newDocumentBuilder().parse(new File(xmlFileName));
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Error setting up UI xml page model parser -- " + e.getMessage());
    } catch (SAXException e) {
        throw new RuntimeException(
                "Error parsing UI xml page model file [" + xmlFileName + "]  -- " + e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(
                "Error opening or reading UI xml page model file [" + xmlFileName + "]  -- " + e.getMessage());
    }
}

From source file:Main.java

/**
 * Returns a <code>DocumentBuilder</code>, which is used for parsing XML
 * documents.//from   ww w. ja v a2  s  .  c o  m
 * 
 * @return a <code>DocumentBuilder</code> which is used for parsing XML
 *         documents. Never <code>null</code>.
 */
public static DocumentBuilder getDocumentBuilder() {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setValidating(false);

        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

/**
 * Parse an XML document./*  w w w.  j ava2  s .c  o  m*/
 *
 * @param is
 *            The input source.
 *
 * @return A Document.
 *
 * @throws SAXException
 *             If there is a parsing error.
 * @throws IOException
 *             If there is an I/O error.
 * @throws IllegalArgumentException
 *             If there is a parser configuration error.
 */
public static Document parse(final InputSource is) throws SAXException, IOException {
    try {
        final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

        return builder.parse(is);
    } catch (final ParserConfigurationException ex) {
        throw new IllegalArgumentException("DOM parser configuration error: " + ex.getMessage());
    }
}

From source file:Main.java

public static Document createDocument() {
    try {/*from   www.j av  a  2s. c  o  m*/
        return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        // How can a vanilla instance throw a configuration exception?
        e.printStackTrace();
        assert false : e.getMessage();
        throw new RuntimeException(e);
    }
}

From source file:gov.nih.nci.cabig.caaers.utils.XmlValidator.java

public static boolean validateAgainstSchema(String xmlContent, String xsdUrl, StringBuffer validationResult) {
    boolean validXml = false;
    try {/*from  w ww.j a v  a  2s.c  om*/
        // parse an XML document into a DOM tree
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setValidating(false);
        documentBuilderFactory.setNamespaceAware(true);
        DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
        Document document = parser.parse(new InputSource(new StringReader(xmlContent)));

        // create a SchemaFactory capable of understanding WXS schemas
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        // load a WXS schema, represented by a Schema instance
        Source schemaFile = new StreamSource(getResources(xsdUrl)[0].getFile());

        Schema schema = schemaFactory.newSchema(schemaFile);
        // create a Validator instance, which can be used to validate an instance document
        Validator validator = schema.newValidator();
        // validate the DOM tree
        validator.validate(new DOMSource(document));
        validXml = true;
    } catch (FileNotFoundException ex) {
        throw new CaaersSystemException("File Not found Exception", ex);
    } catch (IOException ioe) {
        validationResult.append(ioe.getMessage());
        logger.error(ioe.getMessage());
    } catch (SAXParseException spe) {
        validationResult.append("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
        logger.error("Line : " + spe.getLineNumber() + " - " + spe.getMessage());
    } catch (SAXException e) {
        validationResult.append(e.toString());
        logger.error(e.toString());
    } catch (ParserConfigurationException pce) {
        validationResult.append(pce.getMessage());
    }
    return validXml;
}

From source file:com.mycila.xmltool.XMLDocumentBuilderFactory.java

public static DocumentBuilder newDocumentBuilder(boolean ignoreNamespaces) {
    try {/*from   w  w  w.ja  va2 s .c  o m*/
        javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory
                .newInstance();
        factory.setNamespaceAware(!ignoreNamespaces);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new XMLErrorHandler(true));
        builder.setEntityResolver(CachedEntityResolver.instance);
        return builder;
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}

From source file:Main.java

public static Document newXmlDocument() {
    try {//www.jav a2 s.  co m
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        return docBuilder.newDocument();
    } catch (ParserConfigurationException ex) {
        System.err.println("Error: Canot create new XML document");
        System.err.println("Cause: " + ex.getMessage());
        System.exit(1);
        return null;
    }
}

From source file:Main.java

/**
 * Parses the specified XML file and returns it as a {@link Document}.
 *
 * @param xmlFilePath/*w w  w.  j a  v a  2  s.  co m*/
 *            The path to the file to parse.
 * @return The {@code Document} created from the file.
 * @throws SAXException
 *             If any parse error occurs.
 * @throws IOException
 *             If any IO error occurs.
 */
public static Document getDomDocumentFromFile(String xmlFilePath) throws SAXException, IOException {
    try {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        return documentBuilder.parse(fixUri(xmlFilePath));
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Internal error: ParserConfigurationException: " + e.getMessage());
    }
}