public class XMLReader extends DefaultHandler
Used SAX to parse a given XML file. A set of NodeHandler
objects can be given, which get notified if
a sub-tree below a given tag was parsed. This sub-tree is available as DOM and can conveniently be processed
using xpath.
Constructor and Description |
---|
XMLReader()
Creates a new XMLReader.
|
Modifier and Type | Method and Description |
---|---|
void |
addHandler(String name,
NodeHandler handler)
Registers a new handler for a qualified name of a node.
|
void |
characters(char[] ch,
int start,
int length) |
InputSource |
emptyResource() |
void |
endDocument() |
void |
endElement(String uri,
String localName,
String name) |
void |
parse(InputStream stream)
Parses the given stream.
|
void |
parse(InputStream stream,
Function<String,InputStream> resourceLocator)
Parses the given stream using the given locator and interrupt signal.
|
void |
processingInstruction(String target,
String data) |
void |
startElement(String uri,
String localName,
String name,
Attributes attributes) |
endPrefixMapping, error, fatalError, ignorableWhitespace, notationDecl, resolveEntity, setDocumentLocator, skippedEntity, startDocument, startPrefixMapping, unparsedEntityDecl, warning
public XMLReader()
Use addHandler(String, NodeHandler)
tobind handlers to tags and then call one of the parse
methods to process the XML file.
To interrupt processing use TaskContext.cancel()
.
public void characters(char[] ch, int start, int length) throws SAXException
characters
in interface ContentHandler
characters
in class DefaultHandler
SAXException
public void endDocument() throws SAXException
endDocument
in interface ContentHandler
endDocument
in class DefaultHandler
SAXException
public void endElement(String uri, String localName, String name) throws SAXException
endElement
in interface ContentHandler
endElement
in class DefaultHandler
SAXException
public void processingInstruction(String target, String data) throws SAXException
processingInstruction
in interface ContentHandler
processingInstruction
in class DefaultHandler
SAXException
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException
startElement
in interface ContentHandler
startElement
in class DefaultHandler
SAXException
public void addHandler(String name, NodeHandler handler)
Handlers are invoked after the complete node was read. Namespaces are ignored for now which eases the processing a lot (especially for xpath related tasks). Namespaces however could be easily added by replacing String with QName here.
name
- the qualified name of the tag which should be parsed and processedhandler
- the NodeHandler used to process the parsed DOM sub-treepublic void parse(InputStream stream) throws IOException
stream
- the stream to parseIOException
- if parsing the XML fails either due to an IO error or due to an SAXException (when
processing a malformed XML).public void parse(InputStream stream, Function<String,InputStream> resourceLocator) throws IOException
stream
- the stream containing the XML dataresourceLocator
- the resource locator used to discover dependent resourcesIOException
- if parsing the XML fails either due to an IO error or due to an SAXException (when
processing a malformed XML).public InputSource emptyResource()
Copyright © 2018. All rights reserved.