{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14-xml-notes - SAX DOM CPS 116 Introduction to Database...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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 API’s 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
Image of page 1

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

View Full Document Right Arrow Icon
2 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; } … …
Image of page 2
3 7 A simple SAX example (cont’d) 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 8 A common mistake What is wrong with the following?
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern