Issue
I am doing an application, and I need to create 10 TextFields and save value in a XML file that i have. (I know how to save it in XML)
The issue I have is I want to save it all (in a XML) automatically without repeating the code like 10 times for each variable.
I am trying reflect.Field library to do it (Not accomplished) but i don't know if it's the best solution.
public TextField Tf_TestI1;
public TextField Tf_TestF1;
public TextField Tf_TestI2;
public TextField Tf_TestF2;
public TextField Tf_TestI3;
public TextField Tf_TestF3;
public TextField Tf_TestI4;
public TextField Tf_TestF4;
public TextField Tf_TestI5;
public TextField Tf_TestF5;
//Pair them and save it in XML
private void stuffVariables(){
String nameField1= "Tf_TestI";
String nameField2= "Tf_TestF";
Field[] fields = Controller.class.getFields();
for (int i = 0; i <fields.length ; i++) {
if (fields[i].getName().startsWith(nameField1)){
for (int j = 0; j < fields.length ; j++) {
if (fields[j].getName().equals(nameField2+fields[i].getName().substring(fields[i].getName().length()-1))){
System.out.println("EQUALS : "+fields[i].getName() + " = "+ fields[j].getName());
try {
//System.out.println("1: "+fields[i].getValue);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
I am trying to get the pair of Test Initial and Final to finally save it in XML like this...
<TESTS>
<TEST>
<INITIAL>AD</INITIAL>
<FINAL>AVB</FINAL>
</TEST>
<TEST>
<INITIAL>AQEW</INITIAL>
<FINAL>AVFE</FINAL>
</TEST>
<!-- ... MORE TEST -->
<TESTS>
Solution
My main objective was to make it easy when I add more TextFields... Using HashMap I only have to add a Key to his Value. Using numbers in the Key made it more easy in my case.
//[...]
Map<String, TextField> map; //Global variable
//I just have to add the new TextFields to the HashMap
private void mapSaveRangs() {
map = new HashMap<>();
map.put("rangInitial1", tfrangInitial1);
map.put("rangFinal1", tfrangFinal1);
map.put("rangInitial2", tfrangInitial2);
map.put("rangFinal2", tfrangFinal2);
map.put("rangInitial3", tfrangInitial3);
map.put("rangFinal3", tfrangFinal3);
map.put("rangInitial4", tfrangInitial4);
map.put("rangFinal4", tfrangFinal4);
map.put("rangInitial5", tfrangInitial5);
map.put("rangFinal5", tfrangFinal5);
}
private void rangsInPairsXML(Element peeRangs) {
String sKeyrInitial = "rangInitial";
String sValuerInitial;
String sKeyrFinal = "rangFinal";
String sValuerFinal;
//Making sure that we have pair of 2
if (map.size() % 2 == 0) {
//map half size loop
for (int numRang = 1; numRang <= map.size() / 2; numRang++) {
sValuerInitial = map.get(sKeyrInitial + numRang).getText();
sValuerFinal = map.get(sKeyrFinal + numRang).getText();
//Verifying values and saving it in peeRang (XML Element)
if (!(sValuerInitial.equals("") || sValuerFinal.equals(""))) {
Element eRang = new Element("RANG");
eRang.addContent(new Element("INITIAL").addContent(sValuerInitial));
eRang.addContent(new Element("FINAL").addContent(sValuerFinal));
peeRangs.addContent(eRang);
}
}
} else {
System.out.println("ERROR!!");
//[...]
}
}
Answered By - AzmahQi