[go: up one dir, main page]

0% found this document useful (0 votes)
0 views14 pages

Csc241.10 Building An XML Parser

The document provides a guide on building an XML parser using the Java SAX API, which is an event-driven framework for processing XML. It outlines the steps required to set up the parser, including importing libraries, instantiating a SAXParserFactory, and writing a handler for input events. Additionally, it discusses how to create Java objects from XML elements and attributes, detailing the methods involved in processing these elements.

Uploaded by

Pwint Shwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views14 pages

Csc241.10 Building An XML Parser

The document provides a guide on building an XML parser using the Java SAX API, which is an event-driven framework for processing XML. It outlines the steps required to set up the parser, including importing libraries, instantiating a SAXParserFactory, and writing a handler for input events. Additionally, it discusses how to create Java objects from XML elements and attributes, detailing the methods involved in processing these elements.

Uploaded by

Pwint Shwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Building an XML Parser

Dr. James P. Early


SUNY Oswego
Java SAX API
● Java SAX API is a widely used framework for processing XML input
● It is “Event Driven”
○ The appearance of significant input events (start element, end element,
etc.) launch a method
● Requires the developer to write a handler for a specific input
● “Parsing” is the process of accessing and dealing with each input event

Reference
Outline of Steps
1. Import necessary Java SAX libraries
2. Instantiate a SAXParserFactory
3. Use the SAXParserFactory to instantiate a SAXParser
4. Set up our input (File object)
5. Write a Handler
6. Launch the parser with the input file and Handler
Step 1 - Import Java SAX Libraries
The following import statements are typically necessary for instantiating the a Java
SAX Parser

import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

(We’ll add this and the following steps to a Netbeans Project)


Step 2 - Instantiate a SAXParserFactory
In our main() method, add the following:

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

This enables the creation of specific parser instances


Step 3 - Instantiate a SAXParser
The SAXParser works with our handler to process all the input events. It is
instantiated in a try/catch block to recover from any exceptions

SAXParser saxParser = saxParserFactory.newSAXParser();


Step 4 - Set Up Input
The SAXParser requires a File object, so we will create that next. The File
constructor requires a String, namely the file name. We will use a fixed value in
our example, but the file name could be read as input from a Scanner.

File input = new File("holidays.xml");


Step 5 - Write a Handler
The Handler class extends the DefaultHandler defined in the Java SAX API. We’ll
add a skeleton now, and we’ll explore the methods later
class MyHandler extends DefaultHandler{

public void startElement(String uri, String localName, String qName, Attributes


attributes)
throws SAXException {
// we’ll add more later
}
}

In main():
MyHandler handler = new MyHandler();
Step 6 - Launch the Parser
The last step involves launching the parser by supplying the input file, and our
handler written specifically for this particular XML input

saxParser.parse(input, handler);
The Basic main() Method
public static void main(String[] args) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();
File input = new File("holidays.xml");
saxParser.parse(input, handler); //which input file & which hanlder

} catch (Exception e) {
e.printStackTrace();
}
}
Processing Elements
Every time a new element tag is encountered, the SAXParser launches an
appropriate method. Every time the method “fires” the parameter values can be
examined to determine which element (and optionally its attributes) has been
processed.

Methods to investigate
● startElement()
● characters() //text
● endElement()
Using XML to Create Objects
The elements and attributes in an XML document can be used to create Java
objects. We’ll use this XML file for our example:

holidays.xml

Suppose that we wanted to create a Holiday object from each <holiday>


element on the input file.

What would the Holiday class look like?

How would we modify the handler to create objects?


XML Parsing Algorithm
● startElement - If the qName corresponds to the object we wish to create, run
the constructor for the object and store the reference as the current object
○ Optionally, get the attribute names/values and store accordingly
● Characters - Create a string from the character array and store as the current
character string (we’ll need this later)
● endElement - Determine the element qName and store the current character
string in the current object for this variable, using a previously defined “set”
method.
Resources
https://docs.oracle.com/javase/tutorial/jaxp/sax/parsing.html

http://www.journaldev.com/1198/java-sax-parser-example

https://www.tutorialspoint.com/java_xml/java_sax_parse_document.htm

Or, just Google “Java SAX example”

You might also like