Created
August 25, 2013 08:37
-
-
Save ryankurte/6332720 to your computer and use it in GitHub Desktop.
Example of Java SAX document parsing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Hashtable; | |
| import org.xml.sax.Attributes; | |
| import org.xml.sax.SAXException; | |
| import org.xml.sax.helpers.DefaultHandler; | |
| /** | |
| * XML parser for simulation config. | |
| * @author ryank_000 | |
| * | |
| */ | |
| public class ConfigParser extends DefaultHandler { | |
| private Hashtable<String, SimComponent> components; | |
| @Override | |
| public void startDocument() throws SAXException { | |
| //Create data structure. | |
| components = new Hashtable<String, SimComponent>(); | |
| } | |
| @Override | |
| public void startElement(String URI, String localName, String qName, Attributes attr) throws SAXException { | |
| //Handle elements. | |
| System.out.println("Start Element: " + localName + " / " + qName); | |
| //Create components. | |
| if(qName.matches("component")) { | |
| String id = attr.getValue("id"); | |
| System.out.println("id: " + id + " URI: " + URI); | |
| } | |
| } | |
| @Override | |
| public void endElement(String URI, String localName, String qName) throws SAXException { | |
| //End of an element. | |
| System.out.println("End Element: " + localName + " " + qName); | |
| } | |
| @Override | |
| public void endDocument() throws SAXException { | |
| //End of document. | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Configuration { | |
| boolean loaded; // Indicates XML has been correctly loaded. | |
| public void Parse(String name) { | |
| XMLReader r; | |
| File f; | |
| ConfigParser c = new ConfigParser(); | |
| //Create File. | |
| f = new File(name); | |
| //Fetch and configure XML reader. | |
| try { | |
| r = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); | |
| } catch (SAXException e) { | |
| Log.Error("Configurator", e.toString()); | |
| return; | |
| } catch (ParserConfigurationException e) { | |
| Log.Error("Configurator", e.toString()); | |
| return; | |
| } | |
| //Attach XML context handler. | |
| r.setContentHandler(c); | |
| //Attempt parser operation. | |
| try { | |
| r.parse(f.toURI().toURL().toString()); | |
| } catch (MalformedURLException e) { | |
| Log.Error("Configurator", e.toString()); | |
| return; | |
| } catch (IOException e) { | |
| Log.Error("Configurator", e.toString()); | |
| return; | |
| } catch (SAXException e) { | |
| Log.Error("Configurator", e.toString()); | |
| return; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment