import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
![](/Images/OutliningIndicators/None.gif)
// JAXP
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
![](/Images/OutliningIndicators/None.gif)
// SAX
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class TestSAXParsing
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (args.length != 1)
{
System.err.println ("Usage: java TestSAXParsing [filename]");
System.exit (1);
}
// Get SAX Parser Factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// Turn on validation, and turn off namespaces
factory.setValidating(true);
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
parser.parse(new File(args[0]), new MyHandler());
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (ParserConfigurationException e)
{
System.out.println("The underlying parser does not support " +
" the requested features.");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (FactoryConfigurationError e)
{
System.out.println("Error occurred obtaining SAX Parser Factory.");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception e)
{
e.printStackTrace();
}
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
class MyHandler extends DefaultHandler
{
// SAX callback implementations from DocumentHandler, ErrorHandler, etc.
![](/Images/OutliningIndicators/InBlock.gif)
private Writer out;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public MyHandler() throws SAXException
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
out = new OutputStreamWriter(System.out, "UTF8");
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e)
{
throw new SAXException("Error getting output handle.", e);
}
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void startDocument() throws SAXException
{
print("<?xml version=\"1.0\"?>\n");
}
![](/Images/OutliningIndicators/InBlock.gif)
public void startElement(String uri, String localName,
String qName, Attributes atts)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
throws SAXException
{
![](/Images/OutliningIndicators/InBlock.gif)
print("<" + qName);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (atts != null)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for (int i=0, len = atts.getLength(); i<len; i++)
{
print(" " + atts.getQName(i) +
"=\"" + atts.getValue(i) + "\"");
}
}
print(">");
}
![](/Images/OutliningIndicators/InBlock.gif)
public void endElement(String uri, String localName,
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
String qName) throws SAXException
{
print("</" + qName + ">\n");
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void characters(char[] ch, int start, int len) throws SAXException
{
print(new String(ch, start, len));
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private void print(String s) throws SAXException
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
out.write(s);
out.flush();
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (IOException e)
{
throw new SAXException("IO Error Occurred.", e);
}
}
}
posted @
2005-06-08 10:51
Rookie.Zhang
阅读(
663)
评论()
编辑
收藏
举报