From 70a83df6a4518e091a920e7aa9952fd164923412 Mon Sep 17 00:00:00 2001 From: Reto Breitenmoser Date: Mon, 4 Jun 2018 21:53:12 +0200 Subject: [PATCH] [New] added method with InputSource input param --- .../li/strolch/utils/helper/XmlHelper.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java index 1f6ea2449..a688ba41b 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java @@ -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