SAX Demo : SAX « XML « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » XML » SAXScreenshots 
SAX Demo

/*
 * Copyright (c) 2000 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 2nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book (recommended),
 * visit http://www.davidflanagan.com/javaexamples2.
 */

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.AttributeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 
 * This class implements the HandlerBase helper class, which means that it
 * defines all the "callback" methods that the SAX parser will invoke to notify
 * the application. In this example we override the methods that we require.
 
 * This example uses full package names in places to help keep the JAXP and SAX
 * APIs distinct.
 */
public class SAXDemo extends org.xml.sax.HandlerBase {
  /** The main method sets things up for parsing */
  public static void main(String[] argsthrows IOException, SAXException,
      ParserConfigurationException {
    // Create a JAXP "parser factory" for creating SAX parsers
    javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance();

    // Configure the parser factory for the type of parsers we require
    spf.setValidating(false)// No validation required

    // Now use the parser factory to create a SAXParser object
    // Note that SAXParser is a JAXP class, not a SAX class
    javax.xml.parsers.SAXParser sp = spf.newSAXParser();

    // Create a SAX input source for the file argument
    org.xml.sax.InputSource input = new InputSource(new FileReader(args[0]));

    // Give the InputSource an absolute URL for the file, so that
    // it can resolve relative URLs in a <!DOCTYPE> declaration, e.g.
    input.setSystemId("file://" new File(args[0]).getAbsolutePath());

    // Create an instance of this class; it defines all the handler methods
    SAXDemo handler = new SAXDemo();

    // Finally, tell the parser to parse the input and notify the handler
    sp.parse(input, handler);

    // Instead of using the SAXParser.parse() method, which is part of the
    // JAXP API, we could also use the SAX1 API directly. Note the
    // difference between the JAXP class javax.xml.parsers.SAXParser and
    // the SAX1 class org.xml.sax.Parser
    //
    // org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser
    // parser.setDocumentHandler(handler); // Set main handler
    // parser.setErrorHandler(handler); // Set error handler
    // parser.parse(input); // Parse!
  }

  StringBuffer accumulator = new StringBuffer()// Accumulate parsed text

  String servletName; // The name of the servlet

  String servletClass; // The class name of the servlet

  String servletId; // Value of id attribute of <servlet> tag

  // When the parser encounters plain text (not XML elements), it calls
  // this method, which accumulates them in a string buffer
  public void characters(char[] buffer, int start, int length) {
    accumulator.append(buffer, start, length);
  }

  // Every time the parser encounters the beginning of a new element, it
  // calls this method, which resets the string buffer
  public void startElement(String name, AttributeList attributes) {
    accumulator.setLength(0)// Ready to accumulate new text
    // If its a servlet tag, look for id attribute
    if (name.equals("servlet"))
      servletId = attributes.getValue("id");
  }

  // When the parser encounters the end of an element, it calls this method
  public void endElement(String name) {
    if (name.equals("servlet-name")) {
      // After </servlet-name>, we know the servlet name saved up
      servletName = accumulator.toString().trim();
    else if (name.equals("servlet-class")) {
      // After </servlet-class>, we've got the class name accumulated
      servletClass = accumulator.toString().trim();
    else if (name.equals("servlet")) {
      // Assuming the document is valid, then when we parse </servlet>,
      // we know we've got a servlet name and class name to print out
      System.out.println("Servlet " + servletName
          ((servletId != null" (id=" + servletId + ")" "")
          ": " + servletClass);
    }
  }

  /** This method is called when warnings occur */
  public void warning(SAXParseException exception) {
    System.err.println("WARNING: line " + exception.getLineNumber() ": "
        + exception.getMessage());
  }

  /** This method is called when errors occur */
  public void error(SAXParseException exception) {
    System.err.println("ERROR: line " + exception.getLineNumber() ": "
        + exception.getMessage());
  }

  /** This method is called when non-recoverable errors occur. */
  public void fatalError(SAXParseException exceptionthrows SAXException {
    System.err.println("FATAL: line " + exception.getLineNumber() ": "
        + exception.getMessage());
    throw (exception);
  }
}

// Sample XML file

/*
 * <?xml version="1.0" encoding="ISO-8859-1"?>
 
 * <web> <s id="hello_servlet_id"> <name>hello </name> <class>Hello </class>
 * </s>
 
 * </web>
 *  
 */

           
       
Related examples in the same category
1. Parsing XML Files with SAX
2. Duplicates XML Files
3. A Program to Display the Input from a SAX ParserA Program to Display the Input from a SAX Parser
4. SAX Checker
5. A Content Handler to Output a Sorted List as HTML
6. A Content Handler to Output a Sorted List
7. Simple lister - extract name and children tags
8. SAX Tree Validator
9. SAX Tree ViewerSAX Tree Viewer
w_w___w__.__j_av__a___2_s_.___c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.