Escolar Documentos
Profissional Documentos
Cultura Documentos
SAX
Prof. Dr. Ch. Reich
rch@fhfurtwangen.de
http://www.informatik.fhfurtwangen.de/~reich
What is SAX?
● SAX (Simple API for XML Access); not a standard;
www.saxproject.org, www.megginson.com/SAX
● Is event oriented:
– The parser runs through the whole document and informs
the application, when an element is found, the application
is looking for.
– In the application the user defines the event-handler-
classes. There the user has to implement the actions of the
classes.
– An object of the event-handler-class has to be registered
by the parser. The parser can call that object during
parsing.
– Is the parser correctly configured, the parser parses the
XML document line by line. The parse can not go back to
parts of the already parsed documents.
● SAX defines just the interfaces.
SAX-Parser Eigenschaften
● Advantages:
– Parsing of any XML file size is possible.
– Helpful, if no data structure has to be build up in the
application.
– Useful, if only special information of the XML document
is needed.
– Simple and fast
● Disadvantages:
– No access of parts of the document at any time.
– Complex search operations are difficult to implement.
– No access of the DTD
– SAX has only read-only access
– SAX is not supported directly by browsers.
Initialisation of the SAX Parser
public class MyHandler extends DefaultHandler
{
...
File file=new File("XMLDocument.xml");
MyHandler myHandler = new MyHandler();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(file, myHandler);
...
}
Events startDocument()
saxParser endDocument() MyHandler
(SAXParser startElement() (MyHandler
Object) endElement() Object)
characters()
XMLDokument.xml
The Handler-Objekt
● The programmer must implement the
interface org.xml.sax.ContentHandler
class MyHandler implements ContentHandler {
public void startElement() {
// here the implementation
}
}
NamespaceSupport
AttributesImpl
XMLReaderFactory
etc.
●
DefaultHandler: Base class for SAX events.
● AttributesImpl: Attributes of elements can be
read and manipulated.
● NamespaceSupport: Namenspaces can be
generated and added to an element.
class: DefaultHandler
● The DefaultHandler implements the
following interfaces:
– ContentHandler: Handles messages from the
parser about events (e.g. startDocument()).
– DTDHandler: Handles messages from the
parser about DTD related events.
– ErrorHandler: Is called, if errors occur.
– EntityResolver: Resolves external entities.
● To write your own handler, you extend
the class DefaultHandler and overwrite
the handler methods.
DefaultHandler Methods
● Methods for error handling:
– fatalError(SAXParseException e)
– error(SAXParseException e)
– warning(SAXParseException e)
● Further element tag notification:
– InputSource resolveEntity(String publicID,
String systemID)
Resolves external entity definitions.
– void startPrefixMapping(String prefix, String
uri) void endPrefixMapping(String prefix)
Information about start/end of a name space
SAX Parser has to use DTD.
● Default: Non-Validating parser; parser tests
only the well formness of a document.
● saxParserFactory.setValidating(true)
– Befor the SAX parser is generated by the
SAXParserFactory the method: setValidating()
has to be called.
● Implementing public void error
(SAXParseException e)
– Then you can work on errors, which are
generated by inconsistencies appeared using a
DTD.
SAX Parser has to use XSD.
● As before.
– Additionally you have to tell the parser that XSD
has to be used:
parser.setProperty
("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
– Additionally you have to use the XMLReader
interface that can handle events:
XMLReader reader = parser.getXMLReader();
reader.parse( new InputSource("filename"));