From 019dc655a0e59d8ea715437934393b4bf1cf4d2d Mon Sep 17 00:00:00 2001 From: shaper Date: Fri, 20 Jul 2001 22:56:00 +0000 Subject: [PATCH] Initial version of utility class to facilitate XML parsing. git-svn-id: https://samskivert.googlecode.com/svn/trunk@197 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../src/java/com/samskivert/xml/XMLUtil.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/xml/XMLUtil.java diff --git a/projects/samskivert/src/java/com/samskivert/xml/XMLUtil.java b/projects/samskivert/src/java/com/samskivert/xml/XMLUtil.java new file mode 100644 index 00000000..c3c39e51 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/xml/XMLUtil.java @@ -0,0 +1,47 @@ +// +// $Id: XMLUtil.java,v 1.1 2001/07/20 22:56:00 shaper Exp $ + +package com.samskivert.xml; + +import org.xml.sax.*; +import org.xml.sax.helpers.DefaultHandler; +import org.xml.sax.helpers.XMLReaderFactory; + +import java.io.*; +import javax.xml.parsers.*; + +/** + * The XMLUtil class provides a simplified interface for XML parsing. + * + *

Classes wishing to parse XML data should extend the + * org.xml.sax.helpers.DefaultHandler class, override the + * desired SAX event handler methods, and callXMLUtil.parse(). + */ +public class XMLUtil +{ + /** + * Parse the XML data in the given input stream, using the + * specified handler object as both the content and error handler. + * + * @param handler the SAX event handler + * @param in the input stream containing the XML to be parsed + */ + public static void parse (DefaultHandler handler, InputStream in) + throws IOException, ParserConfigurationException, SAXException + { + XMLReader xr = _pfactory.newSAXParser().getXMLReader(); + + xr.setContentHandler(handler); + xr.setErrorHandler(handler); + + xr.parse(new InputSource(in)); + } + + /** The factory from whence we obtain XMLReader objects */ + protected static SAXParserFactory _pfactory; + + static { + _pfactory = SAXParserFactory.newInstance(); + _pfactory.setValidating(false); + } +}