使用SAX解析XML文档

前端之家收集整理的这篇文章主要介绍了使用SAX解析XML文档前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

SAX是一种基于事件驱动的编程方法,SAX解析器对不同类型的内容触发不同的事件。已注册的监听器会对这些事件进行相应。

XmlReader不是靠事件触发,而是依赖于开发人员通过指定的功能来精确定位目标;使用XmlReader的开发者通过游标在文档中移动,当目标内容被找到时会把游标停在相应的位置

import org.xml.sax.*;
import org.xml.sax.helpers.*;

import java.io.*;

public class SaxParser5 extends DefaultHandler {

  private Locator docLocator = null;
  private StringBuffer buffer = new StringBuffer();
  
  public void setDocumentLocator(Locator locator)
  {
    docLocator = locator;
  }
 
  public void startDocument( ) throws SAXException {
    System.out.println( "SAX Event: START DOCUMENT" );
  }

  public void endDocument( ) throws SAXException {
    System.out.println( "SAX Event: END DOCUMENT" );
  }

  public void startElement(String namespaceURI,String localName,String qName,Attributes attr ) throws SAXException {
    int lineNumber = 0;
    if (docLocator != null)
    {
      lineNumber = docLocator.getLineNumber();      
    }    
    System.out.println( "SAX Event: START ELEMENT[ " + localName + " ]");
    if (lineNumber != 0)
    {
      System.out.println("\t(Found at line number: " + lineNumber + ".)");
    }
    for ( int i = 0; i < attr.getLength(); i++ ){
    System.out.println( " ATTRIBUTE: " + attr.getLocalName(i) +  " VALUE: " + attr.getValue(i) );
    }
    
    buffer.setLength(0);
  }

  public void endElement(String namespaceURI,String qName ) throws SAXException {
    System.out.print( "SAX Event: CHARACTERS[ " );
    System.out.println(buffer.toString());
    System.out.println( " ]" );     
    System.out.println( "SAX Event: END ELEMENT[ " + localName + " ]" );
  }

  public void characters(char[] ch,int start,int length ) throws SAXException {
    try {
      buffer.append(ch,start,length); 
    } catch (Exception e) {
     e.printStackTrace();
    }
  }

  public void warning (SAXParseException exception)
    throws SAXException {
    System.err.println("[Warning] " +
      exception.getMessage() + " at line " +
      exception.getLineNumber() + ",column " +
      exception.getColumnNumber() );
  }

  public void error (SAXParseException exception)
    throws SAXException {
    System.err.println("[Error] " +
      exception.getMessage() + " at line " +
      exception.getLineNumber() + ",column " +
      exception.getColumnNumber() );
  }

  public void fatalError (SAXParseException exception)
    throws SAXException {
    System.err.println("[Fatal Error] " +
      exception.getMessage() + " at line " +
      exception.getLineNumber() + ",column " +
      exception.getColumnNumber() );
    throw exception;
  }
  

  public static void main( String[] argv ){
	  String inputFile = "..\\workspace\\Test\\bin\\People.xml";
    System.out.println("Processing '" + inputFile + "'.");
    System.out.println( "SAX Events:" );
    System.setProperty("entityExpansionLimit","123876124"); //设定的单文件实体引用数最大值。
   try {
      XMLReader reader = XMLReaderFactory.createXMLReader();
      SaxParser5 parser = new SaxParser5();
      reader.setContentHandler(parser);
      reader.setErrorHandler(parser);
      try
      {
        reader.setFeature("http://xml.org/sax/features/validation",true);
      } catch (SAXException e) {
      System.err.println("Cannot activate validation");
      }

      reader.parse( new InputSource(
                 new FileReader( inputFile )));
    }catch ( Exception e ) {
       e.printStackTrace();
    }
  }
}
原文链接:https://www.f2er.com/xml/298956.html

猜你在找的XML相关文章