Java XML Parse String parseXML(String pathToMap)

Here you can find the source of parseXML(String pathToMap)

Description

Given a path to an XML instance parse it into a Document.

License

Apache License

Parameter

Parameter Description
pathToMap - the file to parse. If there is DTD associated with the file it will be used to validate the instance.

Exception

Parameter Description
ParserConfigurationException non recoverable try again with better data.
SAXException non recoverable try again with better data.
IOExceptionnon recoverable check that file exists

Return

a org.w3c.dom.Document

Declaration

public static Document parseXML(String pathToMap)
        throws ParserConfigurationException, SAXException, IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

public class Main {
    /**/*from   w  ww .  java2s . c o m*/
     * Given a path to an XML instance parse it into a Document.
     *
     * @param pathToMap - the file to parse. If there is DTD associated with the file it will be used to validate the
     *                  instance.
     * @return a org.w3c.dom.Document
     * @throws ParserConfigurationException non recoverable try again with better data.
     * @throws SAXException                 non recoverable try again with better data.
     * @throws IOException                  non recoverable check that file exists
     */
    public static Document parseXML(String pathToMap)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setValidating(true);
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {
            @Override
            public void error(SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void fatalError(SAXParseException exception) throws SAXException {
                throw exception;

            }

            @Override
            public void warning(SAXParseException exception) throws SAXException {
                throw exception;
            }
        });
        return builder.parse(pathToMap);
    }
}

Related

  1. parseArray(String array)
  2. parseArray(String array)
  3. parseArray(String inputArray)
  4. parseXml(Object obj)
  5. parseXML(String path)
  6. parseXML(String resp, String name)
  7. parseXML(String text)
  8. parseXml(String uri)
  9. parseXml(String xml)