Implement additional XML readers
dizzzz opened this issue · 2 comments
dizzzz commented
Inspiration on http://www.jfree.org/phpBB2/viewtopic.php?f=3&t=28009
public class CustomDatasetReader extends DatasetReader {
public static XYDataset readXYDatasetFromXML(InputStream in) throws IOException {
XYDataset result = null;
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
XYDatasetHandler handler = new XYDatasetHandler();
parser.parse(in, handler);
result = handler.getDataset();
} catch (SAXException e) {
System.out.println(e.getMessage());
} catch (ParserConfigurationException e2) {
System.out.println(e2.getMessage());
}
return result;
}
}
and
public class XYDatasetHandler extends RootHandler implements DatasetTags {
public void addItem(Comparable rowKey, Comparable columnKey, Number value) {
TimeSeries timeSeries = this.dataset.getSeries(rowKey);
if (timeSeries == null) {
timeSeries = new TimeSeries(rowKey);
this.dataset.addSeries(timeSeries);
}
try {
timeSeries.add(new Day(dateFormat.parse(columnKey.toString())), value);
} catch (ParseException exc) {
// just in case the date format is wrong.
timeSeries.add(new Day(), value);
System.out.println("Date format is incorrect: " + columnKey);
}
}
}
ljo commented
Yes, looks nice. Definitely a good-to-have feature.