A Program to convert tab delimited data to XML
import java.io.*;
import org.jdom.*;
import org.jdom.output.XMLOutputter;
public class JDOMBaseballTabToXML {
public static void main(String[] args) {
Element root = new Element("players");
try {
FileInputStream fin = new FileInputStream(args[0]);
BufferedReader in
= new BufferedReader(new InputStreamReader(fin));
String playerStats;
while ((playerStats = in.readLine()) != null) {
String[] stats = splitLine(playerStats);
Element player = new Element("player");
Element first_name = new Element("first_name");
first_name.setText(stats[1]);
player.addContent(first_name);
Element surname = new Element("surname");
surname.setText(stats[0]);
player.addContent(surname);
Element games_played = new Element("games_played");
games_played.setText(stats[4]);
player.addContent(games_played);
Element at_bats = new Element("at_bats");
at_bats.setText(stats[6]);
player.addContent(at_bats);
Element runs = new Element("runs");
runs.setText(stats[7]);
player.addContent(runs);
Element hits = new Element("hits");
hits.setText(stats[8]);
player.addContent(hits);
Element doubles = new Element("doubles");
doubles.setText(stats[9]);
player.addContent(doubles);
Element triples = new Element("triples");
triples.setText(stats[10]);
player.addContent(triples);
Element home_runs = new Element("home_runs");
home_runs.setText(stats[11]);
player.addContent(home_runs);
Element runs_batted_in = new Element("runs_batted_in");
runs_batted_in.setText(stats[12]);
player.addContent(runs_batted_in);
Element stolen_bases = new Element("stolen_bases");
stolen_bases.setText(stats[13]);
player.addContent(stolen_bases);
Element caught_stealing = new Element("caught_stealing");
caught_stealing.setText(stats[14]);
player.addContent(caught_stealing);
Element sacrifice_hits = new Element("sacrifice_hits");
sacrifice_hits.setText(stats[15]);
player.addContent(sacrifice_hits);
Element sacrifice_flies = new Element("sacrifice_flies");
sacrifice_flies.setText(stats[16]);
player.addContent(sacrifice_flies);
Element errors = new Element("errors");
errors.setText(stats[17]);
player.addContent(errors);
Element passed_by_ball = new Element("passed_by_ball");
passed_by_ball.setText(stats[18]);
player.addContent(passed_by_ball);
Element walks = new Element("walks");
walks.setText(stats[19]);
player.addContent(walks);
Element strike_outs = new Element("strike_outs");
strike_outs.setText(stats[20]);
player.addContent(strike_outs);
Element hit_by_pitch = new Element("hit_by_pitch");
hit_by_pitch.setText(stats[21]);
player.addContent(hit_by_pitch);
root.addContent(player);
}
Document doc = new Document(root);
// serialize it into a file
FileOutputStream fout
= new FileOutputStream("baseballstats.xml");
XMLOutputter serializer = new XMLOutputter(" ", true);
serializer.output(doc, fout);
fout.flush();
fout.close();
in.close();
}
catch (IOException e) {
System.err.println(e);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Usage: java BaseballTabToXML input_file.tab");
}
}
public static String[] splitLine(String playerStats) {
// count the number of tabs
int numTabs = 0;
for (int i = 0; i < playerStats.length(); i++) {
if (playerStats.charAt(i) == '\t') numTabs++;
}
int numFields = numTabs + 1;
String[] fields = new String[numFields];
int position = 0;
for (int i = 0; i < numFields; i++) {
StringBuffer field = new StringBuffer();
while (position < playerStats.length()
&& playerStats.charAt(position++) != '\t') {
field.append(playerStats.charAt(position-1));
}
fields[i] = field.toString();
}
return fields;
}
}
View Output in Browser