We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello,
I try to convert a table in csv, which is like a tree strcuture, into a xml format.
is it possible ? I don't really get it
cheers
XML xml;
Table table ;
void setup() {
xml = new XML("science");
table = loadTable("data.csv", "header, tsv");
XML child = xml.addChild("area");
child.setString("name", "Chemistry");
for ( int i=1; i < table.getRowCount(); i++) {
// if fiefd area in table is equal to child in xml
if (table.getString(i, "area").equals(child.getString("name"))) {
println(child.getString("name"));
// then add field subject present in table to xml new sub child
XML subChild = child.addChild("subject");
subChild.setContent(table.getString(i, "subject"));
}
}
println(xml);
}
Answers
can you post the csv or relevant parts of it?
of course, the separator is tab, a header is present :
you could first insert all words from the 1st col into a hashmap (see reference)
each word will be unique then
then loop over the hashmap and for each word in it check the 2nd col and build the xml
That's not a .csv file (CSV = Comma Separated Values). I can't even spot a single comma there! :-?
here is the data, with Comma separator thanks
okay, thanks, but we don't get the tree structure.
I would like to have
so we have to add the children
<subject>
inside the children<area>
thanks,
but still we havent a tree structure. as before area and subject are present in the same element.
<area name="Computer Science Artificial Intelligence">
whereas I would like to havethe
<subject>
is a child of<area>
, it is a subfield.Maybe make scribble
@mxloizix, are you sure you really checked the "science.xml" output from "data.csv" input? 8-|
okay, my apologies, my data was in tsv so the output wasn't the same.
it is perfect
thank you for your patience
That's OK! I believe this lil' change from:
final Table csv = loadTable("data.csv", "header, csv");
to:final Table csv = loadTable("data.tsv", "header, tsv");
can allow to read your original file. :bz
yeah,luckily I get the Table class :)
whereas this
Map<String, Set<String>> areas = new LinkedHashMap<String, Set<String>>();
is more difficult for me :)thx again
Unfortunately that complex Map was necessary in order to group all subjects under their respective area tag. ^#(^ Otherwise it would end up w/ 29 area tags w/ 1 subject inside each 1. :-&
Since I'm trying to learn Python/Jython via Python Mode, decided to convert the Java Mode.
Much less boiler-plate for sure: B-) Although I had to import almost everything! X(