看看这个,对你可能会有帮助的:
其中的处理程序,可根据自己的情况填写import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.XMLReaderFactory;
import org.apache.xerces.parsers.SAXParser;public class ParseXMLDoc
{
  public void ShowParseResult(String uri)
  {
  System.out.println("Parse " + uri + " file!!!");
  System.out.println("========================================");
  ContentHandler CHandler = new CHandlerClass();  try
    {
    XMLReader MyParser = XMLReaderFactory.createXMLReader(
    "org.apache.xerces.parsers.SAXParser");    MyParser.setContentHandler(CHandler);
    MyParser.parse(uri);
    }
  catch (IOException e)
    {
    System.out.println("Error IOException!!!");
    }
  catch (SAXException e)
    {
    System.out.println("Error SAXException!!!");
    }
  }  public static void main(String[] args)
  {
  String uri = args[0];  ParseXMLDoc ParseXMLInstance = new ParseXMLDoc();
  ParseXMLInstance.ShowParseResult(uri);
  }
}class CHandlerClass implements ContentHandler
{
private Locator locator;  public void setDocumentLocator(Locator locator)
  {
  System.out.println("The Parser called setDocumentLocator()!!!");
  this.locator = locator;
  System.out.println("========================================");
  }  public void startDocument() throws SAXException
  {
  System.out.println("The Parser called startDocument()!!!");
  System.out.println("========================================");
  }  public void endDocument() throws SAXException
  {
  System.out.println("The Parser called endDocument()!!!");
  System.out.println("========================================");
  }  public void processingInstruction(String target, String data)
  throws SAXException
  {
  System.out.println("The Parser called processingInstruction()!!!");
  System.out.println("target: [" + target + "] data: [" + data + "]");
  System.out.println("========================================");
  }  public void startPrefixMapping(String prefix, String uri)
  {
  System.out.println("The Parser called startPrefixMapping()!!!");
  System.out.println("prefix: [" + prefix + "] uri: " + uri + "]");
  System.out.println("========================================");
  }  public void endPrefixMapping(String prefix)
  {
  System.out.println("The Parser called endPrefixMapping()!!!");
  System.out.println("prefix: [" + prefix + "]");
  System.out.println("========================================");
  }  public void startElement(String namespaceURI, String localName,
  String rawName, Attributes atts) throws SAXException
  {
  System.out.println("The Parser called startElement()!!!");  if (!namespaceURI.equals(""))
    {
    System.out.println("namespaceURI: [" + namespaceURI +
    "] rawName: [" + rawName + "]");
    }
  else
    {
    System.out.println("There is no namespace");
    }  System.out.println("localName: [" + localName + "]");  for (int i = 0; i < atts.getLength(); i++)
    {
    System.out.println("Attribute [" + atts.getLocalName(i) +
    "] is [" + atts.getValue(i) + "]");
    }  System.out.println("========================================");
  }  public void endElement(String namespaceURI,
  String localName, String rawName) throws SAXException
  {
  System.out.println("The Parser called endElement()!!!");
  System.out.println("localName: [" + localName + "]");
  System.out.println("========================================");
  }  public void characters(char[] ch, int start, int end)
  throws SAXException
  {
  String sss = new String(ch, start, end);
  
  System.out.println("The Parser called characters()!!!");
  System.out.println("characters: [" + sss + "]");
  System.out.println("========================================");
  }  public void ignorableWhitespace(char[] ch, int start, int end)
  throws SAXException
  {
  String sss = new String(ch, start, end);  System.out.println("The Parser called ignorableWhitespace()!!!");
  System.out.println("ignorableWhitespace: [" + sss + "]");
  System.out.println("========================================");
  }  public void skippedEntity(String name) throws SAXException
  {
  System.out.println("The Parser called skippedEntity()!!!");
  System.out.println("skippedEntity is [" + name + "]");
  System.out.println("========================================");
  }
}