java.lang.Object
org.xml.sax.helpers.DefaultHandler
- All Implemented Interfaces:
- ContentHandler, DTDHandler, EntityResolver, ErrorHandler
- Direct Known Subclasses:
- DefaultHandler2
- See Also:
- Top Examples, Source Code,
HandlerBase
public void characters(char[] ch,
int start,
int length)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[676]Process characters from SAX handler
By nguyenvtri { at } yahoo { dot } com on 2004/03/02 07:01:31 Rate
public void characters ( char buf [ ] , int offset, int len )
//throws SAXException
{
System.out.println ( "---- > > COME IN---- > > " ) ;
String s = new String ( buf, offset, len ) ;
System.out.println ( "length=" + len ) ;
System.out.println ( "--OUT: " + s ) ;
if ( s.indexOf ( "\n" ) < 0 && s.length ( ) > 0 ) {
this.m_sCurrentNodeValue = s;
System.out.println ( "---IN: " + s ) ;
}
System.out.println ( " < < ----COME OUT < < ----" ) ;
}
public DefaultHandler()
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void endDocument()
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void endElement(String uri,
String localName,
String qName)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void endPrefixMapping(String prefix)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void error(SAXParseException e)
throws SAXException
- See Also:
-
ErrorHandler.warning(org.xml.sax.SAXParseException)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void fatalError(SAXParseException e)
throws SAXException
- See Also:
- ErrorHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void ignorableWhitespace(char[] ch,
int start,
int length)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void notationDecl(String name,
String publicId,
String systemId)
throws SAXException
- See Also:
- DTDHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void processingInstruction(String target,
String data)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public InputSource resolveEntity(String publicId,
String systemId)
throws IOException,
SAXException
- See Also:
- EntityResolver
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void setDocumentLocator(Locator locator)
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void skippedEntity(String name)
throws SAXException
- See Also:
ContentHandler.processingInstruction(java.lang.String, java.lang.String)
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void startDocument()
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void startElement(String uri,
String localName,
String qName,
Attributes attributes)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1581]SAX helper class captures individual element and attribute values
By Anonymous on 2005/11/04 20:23:08 Rate
public abstract class SAXInterpreter
extends DefaultHandler
{
public abstract void element ( String name, String value, Stack path ) ;
public abstract void attribute ( String name, String value, Stack path ) ;
public void startElement ( String URI, String localName,
String qName, Attributes attributes )
throws SAXException
{
path.push ( qName ) ;
int length = attributes.getLength ( ) ;
for ( int a = 0; a < length; ++a )
attribute ( attributes.getQName ( a ) ,
attributes.getValue ( a ) , path ) ;
elementValue = new StringBuffer ( ) ;
}
public void characters ( char [ ] chars, int start, int length )
throws SAXException
{
if ( elementValue != null )
elementValue.append ( chars, start, length ) ;
}
public void endElement ( String URI, String localName, String qName )
throws SAXException
{
path.pop ( ) ;
// If elementValue is null, this is a mixed or complex element.
// If it's "", the element is empty.
if ( elementValue != null )
element ( qName, elementValue.toString ( ) , path ) ;
elementValue = null;
}
protected static String XPath ( Stack path )
{
StringBuffer result = new StringBuffer ( "//" ) ;
Iterator each = path.iterator ( ) ;
while ( each.hasNext ( ) )
result.append ( ( String ) each.next ( ) )
.append ( '/' ) ;
return result.toString ( ) ;
}
protected Collection getPath ( )
{
return new Vector ( path ) ;
}
private Stack path = new Stack ( ) ;
private StringBuffer elementValue;
}
public void startPrefixMapping(String prefix,
String uri)
throws SAXException
- See Also:
- ContentHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void unparsedEntityDecl(String name,
String publicId,
String systemId,
String notationName)
throws SAXException
- See Also:
- DTDHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
public void warning(SAXParseException e)
throws SAXException
- See Also:
- ErrorHandler
- Geek's Notes:
- Description Add your codes or notes Search More Java Examples
[1577]Use SAX to re-generate XML file
By Anonymous on 2005/11/04 20:23:08 Rate
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class PrettyPrinter
extends DefaultHandler
{
public static void main ( String [ ] args )
{
try
{
SAXParserFactory factory = SAXParserFactory.newInstance ( ) ;
try
{
factory.setFeature
( "http://xml.org/sax/features/namespace-prefixes", true ) ;
}
catch ( SAXException ex )
{
ex.printStackTrace ( ) ;
}
parser = factory.newSAXParser ( ) ;
if ( args.length == 0 )
parser.parse ( System.in, new PrettyPrinter ( ) ) ;
else
parser.parse ( new File ( args [ 0 ] ) , new PrettyPrinter ( ) ) ;
}
catch ( ParserConfigurationException ex )
{
ex.printStackTrace ( System.err ) ;
}
catch ( SAXException ex2 )
{
ex2.printStackTrace ( System.err ) ;
}
catch ( IOException ex3 )
{
ex3.printStackTrace ( System.err ) ;
}
}
public void startDocument ( )
throws SAXException
{
System.out.println ( " < ?xml version=\"1.0\" encoding=\"UTF-8\" ? > " ) ;
}
public void endDocument ( ) throws SAXException
{
System.out.println ( ) ;
}
public void startElement
( String URI, String name, String qName, Attributes attributes )
throws SAXException
{
StringBuffer output = new StringBuffer ( ) ;
output.append ( endLine )
.append ( indent )
.append ( ' < ' )
.append ( qName ) ;
int length = attributes.getLength ( ) ;
for ( int a = 0; a < length; ++a )
output.append ( endLine )
.append ( indent )
.append ( standardIndent )
.append ( attributes.getQName ( a ) )
.append ( "=\"" )
.append ( attributes.getValue ( a ) )
.append ( '\"' ) ;
if ( length > 0 )
output.append ( endLine )
.append ( indent ) ;
output.append ( ' > ' ) ;
System.out.print ( output.toString ( ) ) ;
indent += standardIndent;
currentValue = new StringBuffer ( ) ;
}
public void endElement ( String URI, String name, String qName )
throws SAXException
{
indent = indent.substring
( 0, indent.length ( ) - standardIndent.length ( ) ) ;
if ( currentValue == null )
{
currentValue = new StringBuffer ( ) ;
currentValue.append ( endLine )
.append ( indent ) ;
}
currentValue.append ( " < /" )
.append ( qName )
.append ( ' > ' ) ;
System.out.print ( currentValue.toString ( ) ) ;
currentValue = null;
}
public void characters ( char [ ] chars, int start, int length )
throws SAXException
{
if ( currentValue != null )
currentValue.append ( escape ( chars, start, length ) ) ;
}
private static String escape ( char [ ] chars, int start, int length )
{
StringBuffer result = new StringBuffer ( ) ;
for ( int c = start; c < start + length; ++c )
if ( chars [ c ] == ' < ' )
result.append ( "<" ) ;
else if ( chars [ c ] == '&' )
result.append ( "&" ) ;
else
result.append ( chars [ c ] ) ;
return result.toString ( ) ;
}
private String indent = "";
private StringBuffer currentValue = null;
static SAXParser parser;
private static final String endLine = "\r\n";
private static final String standardIndent = " ";
}
[1580]Create a SAX parser that can validate against DTDs or Schema
By Anonymous on 2005/11/04 20:23:08 Rate
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class SAXValidator
extends DefaultHandler
{
public void warning ( SAXParseException ex )
throws SAXException
{
System.out.println
( "Warning at Line " + locator.getLineNumber ( ) + ":" ) ;
System.out.println ( " " + ex.getMessage ( ) ) ;
}
public void error ( SAXParseException ex )
throws SAXException
{
System.out.println
( "Error at Line " + locator.getLineNumber ( ) + ":" ) ;
System.out.println ( " " + ex.getMessage ( ) ) ;
}
public void setDocumentLocator ( Locator locator )
{
this.locator = locator;
}
public static void main ( String [ ] args )
{
try
{
if ( args.length == 0 )
{
System.out.println ( "Usage: " +
"java SAXValidator < XML filename > " ) ;
System.exit ( -1 ) ;
}
SAXParser parser = getSchemaValidatingSAXParser ( ) ;
parser.parse ( new java.io.File ( args [ 0 ] ) , new SAXValidator ( ) ) ;
}
catch ( Exception ex )
{
ex.printStackTrace ( ) ;
}
}
public static SAXParser getSchemaValidatingSAXParser ( )
{
SAXParser result = null;
try
{
SAXParserFactory factory = SAXParserFactory.newInstance ( ) ;
factory.setValidating ( true ) ;
factory.setFeature
( "http://xml.org/sax/features/namespaces", true ) ;
result = factory.newSAXParser ( ) ;
result.setProperty
( "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema" ) ;
}
catch ( Exception ex )
{
System.out.println ( "This parser apparently can't " +
"validate against XML Schema." ) ;
System.out.println ( " ( " + ex.getMessage ( ) + " ) " ) ;
}
return result;
}
private Locator locator;
}