Skip to content
Snippets Groups Projects
Commit f80fbd00 authored by Goik Martin's avatar Goik Martin
Browse files

mixed content sax example

parent 9dca3aa5
No related branches found
No related tags found
No related merge requests found
package sax.mixed;
/** Driver */
public class Driver {
/** @param argv unused */
public static void main(String argv[]) {
try{
MixedCount xmlStats = new MixedCount();
xmlStats.parse("src/main/java/sax/mixed/mixed.xml");
System.out.println("Document contains "
+ xmlStats.getElementCount() + " text nodes within mixed content elements");
} catch (Exception e){
e.printStackTrace(System.err);
}
}
}
\ No newline at end of file
package sax.mixed;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/** Parser, content- and errorhandler assembly */
public class MixedCount {
/** @throws SAXException
* @throws ParserConfigurationException */
public MixedCount()
throws SAXException, ParserConfigurationException{
final SAXParserFactory saxPf = SAXParserFactory.newInstance();
final SAXParser saxParser = saxPf.newSAXParser();
xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(eventHandler);
xmlReader.setErrorHandler(errorHandler);
}
/** @see sax.stat.v1.ElementCount
* @param uri
* @throws IOException
* @throws SAXException */
public void parse(final String uri)
throws IOException, SAXException{
xmlReader.parse(uri);
}
/** @return @see sax.stat.v1.ElementCount#getElementCount() */
public int getElementCount() {
return eventHandler.getMixedElementCount();
}
private final XMLReader xmlReader;
private final MixedCountHandler eventHandler = new MixedCountHandler();
private final MyErrorHandler errorHandler = new MyErrorHandler();
}
\ No newline at end of file
package sax.mixed;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MixedCountHandler extends DefaultHandler {
enum EventType {openElement, closeElement, character, idle};
EventType lastEvent = EventType.idle;
private int mixedElementCount = 0;
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (lastEvent == EventType.character) {
mixedElementCount++;
}
lastEvent = EventType.openElement;
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
lastEvent = EventType.closeElement;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
final String content = new String(ch, start, length);
// We ignore whitespace completely
if (0 < content.trim().length()) {
if (lastEvent == EventType.closeElement) {
mixedElementCount++;
}
lastEvent = EventType.character;
}
}
public int getMixedElementCount() {
return mixedElementCount;
}
}
\ No newline at end of file
package sax.mixed;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ErrorHandler;
/** Handle parsing errors */
public class MyErrorHandler implements ErrorHandler {
/** @see ErrorHandler#warning(SAXParseException) */
public void warning(SAXParseException e) {
System.err.println("[Warning]" + getLocationString(e));
}
/** @see ErrorHandler#error(SAXParseException) */
public void error(SAXParseException e) {
System.err.println("[Error]" + getLocationString(e));
}
/** @see ErrorHandler#fatalError(SAXParseException) */
public void fatalError(SAXParseException e) throws SAXException{
System.err.println("[Fatal Error]" + getLocationString(e));
}
private String getLocationString(SAXParseException e) {
return " line " + e.getLineNumber() +
", column " + e.getColumnNumber()+ ":" + e.getMessage();
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<top>
<a>Test 1</a>
<b>Some <c>mixed</c> content</b>
<d><nothingMixed/></d>
<d> <nothingMixed/> </d>
</top>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment