14-xml - 1 SAX & DOM CPS 116 Introduction to Database...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 SAX & DOM CPS 116 Introduction to Database Systems 2 Announcements (Thu. Oct. 27) Homework #3 due in 1 week Project milestone #2 due in 2 weeks 3 SAX & DOM Both are APIs for XML processing SAX (Simple API for XML) Started out as a Java API, but now exists for other languages too DOM (Document Object Model) Language-neutral API with implementations in Java, C ++ , etc. ) JAXP (Java API for XML Processing) Bundled with standard JDK Includes SAX, DOM parsers and XSLT transformers 4 SAX processing model Serial access XML document is processed as a stream Only one look at the data Cannot go back to an early portion of the document Event-driven A parser generates events as it goes through the document (e.g., start of the document, end of an element, etc.) Application defines event handlers that get invoked when events are generated 5 SAX events Most frequently used events: startDocument endDocument startElement endElement characters Whenever the parser has processed a chunk of character data (without generating other kinds of events) Warning: The parser may generate multiple characters events for one piece of text <?xml version=1.0?> <bibliography> <book ISBN=ISBN-10 price=80.00> <title>Foundations of Databases</title> </book> </bibliography> startElement startDocument startElement endElement endElement endDocument startElement characters endElement Whitespace may come up as characters or ignorableWhitespace , depending on whether a DTD is present 6 A simple SAX example Print out text contents of title elements import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.XMLReaderFactory; import org.xml.sax.helpers.DefaultHandler; public class SaxExample extends DefaultHandler { public static void main(String argv) throws Exception { String fileName = argv[0]; // Create a SAX parser: XMLReader xr = XMLReaderFactory.createXMLReader(); // Parse the document with this event handler: xr.setContentHandler(new SaxExample()); xr.parse(new InputSource(new FileReader(fileName))); return; } 2 7 A simple SAX example (contd) private StringBuffer titleStringBuffer = null; public void startElement(String uri, String localName, String qName, Attributes attributes) { if (qName.equals(title)) titleStringBuffer = new StringBuffer(); } public void endElement(String uri, String localName, String qName) { if (qName.equals(title)) { System.out.println(titleStringBuffer.toString()); titleStringBuffer = null; } } public void characters(char ch, int start, int length) { if (titleStringBuffer != null) titleStringBuffer.append(ch, start, length); } Warning: This code does not handle data with //title[//title] pattern Only relevant when namespace is involved Assuming no namespace processing, qname is tag name...
View Full Document

This document was uploaded on 01/17/2012.

Page1 / 4

14-xml - 1 SAX & DOM CPS 116 Introduction to Database...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online