FileDocCategorySizeDatePackage
csv.javaAPI DocExample2193Fri Feb 01 12:51:50 GMT 2002None

csv

public class csv extends Object
Producing SAX2 Events from CSV Input

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] argv)

	BufferedReader	in;
	XMLWriter	out;
	ErrorHandler	errs;
	String		line;

	try {
	    in = new BufferedReader (new InputStreamReader (System.in));
	    out = new XMLWriter (System.out);
	    errs = new DefaultHandler () {
		    public void fatalError (SAXParseException e) {
			System.err.println ("** parse error: "
			    + e.getMessage ());
		    }
		};

	    out.startElement ("", "", "yoga", new AttributesImpl ());
	    while ((line = in.readLine ()) != null)
		parseLine (line, out, errs);
	    out.endElement ("", "", "yoga");
	    out.flush ();
	} catch (Exception e) {
	    System.err.println ("** error: " + e.getMessage ());
	    e.printStackTrace (System.err);
	    System.exit (1);
	}
    
private static voidparseLine(java.lang.String line, org.xml.sax.ContentHandler out, org.xml.sax.ErrorHandler errs)

	StringTokenizer	tokens = new StringTokenizer (line.trim (), ",");
	String		values [] = new String [5];

	// if there aren't five values, it's malformed
	if (tokens.countTokens () != 5) {
	    errs.fatalError (
		new SAXParseException ("not enough values", null));
	    return;
	}
	for (int i = 0; i < 5; i++)
	    values [i] = tokens.nextToken ();

	// now that we parsed the line safely, report its contents

	// the AttributesImpl class is shown later
	AttributesImpl	atts = new AttributesImpl ();

	atts.addAttribute ("", "", "teacher", "CDATA", values [0]);
	atts.addAttribute ("", "", "time", "CDATA", values [1]);
	atts.addAttribute ("", "", "type", "CDATA", values [2]);
	atts.addAttribute ("", "", "date", "CDATA", values [3]);
	atts.addAttribute ("", "", "level", "CDATA", values [4]);

	out.ignorableWhitespace ("\n  ".toCharArray (), 0, 3);
	out.startElement ("", "", "class", atts);
	out.endElement ("", "", "class");