[New] added method with InputSource input param

This commit is contained in:
Reto Breitenmoser 2018-06-04 21:53:12 +02:00
parent 3f67ee6292
commit 70a83df6a4
1 changed files with 25 additions and 0 deletions

View File

@ -32,6 +32,7 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@ -103,6 +104,30 @@ public class XmlHelper {
throw new XmlException("The XML stream not be read: " + e.getMessage(), e); //$NON-NLS-1$
}
}
/**
* Parses an XML file on the file system and returns the resulting {@link Document} object
*
* @param xmlInputSource
* the XML {@link InputSource} which is to be parsed
*/
public static void parseDocument(InputSource xmlInputSource, DefaultHandler xmlHandler) {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
sp.parse(xmlInputSource, xmlHandler);
} catch (ParserConfigurationException e) {
throw new XmlException("Failed to initialize a SAX Parser: " + e.getMessage(), e); //$NON-NLS-1$
} catch (SAXException e) {
throw new XmlException("The XML stream is not parseable: " + e.getMessage(), e); //$NON-NLS-1$
} catch (IOException e) {
throw new XmlException("The XML stream not be read: " + e.getMessage(), e); //$NON-NLS-1$
}
}
/**
* Writes an {@link Element} to an XML file on the file system