Sunday, November 21, 2010

SAX Example

 Sample s=new Sample();  
 SAXParserFactory factory = SAXParserFactory.newInstance();  
 SAXParser saxParser = factory.newSAXParser();  
 saxParser.parse("Have to give some class object(consider as Sample.java)", inputStream);  
 inputStream.close();  

 public class Sample extends DefaultHandler{  
      public void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException   
      {  
           // This function invokes when parser reads the starting tag  
           // qName is element name  
           // below command gets the value of the Attribute "name"  
          attributes.getValue("name");    
      }  
      public void endElement(String uri, String localName, String qName) throws SAXException {  
           // This function is invoked when the parser reads the ending tag  
      }  
      public void characters(char[] chars, int start, int length) throws SAXException {  
           // Used to read the cdata  
          String s=new String(chars, start,length); // to read CDATA     
      }  
           //Put this line in any of the above method to voluntary exit the
           //parsing  "throw new DoneParsingException();"
 }  

No comments: