csvpublic class csv extends Object Producing SAX2 Events from CSV Input |
Methods Summary |
---|
public static void | main(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 void | parseLine(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");
|
|