Reputation: 121
I have an excel file like this :
I want to get both of the values by using a for-loop and then access them from outside the for-loop. Both will be used as a value of xml element. I have written a code like this :
String longSec = null;
Double long_sec = null;
int column_long_sec = 33;
int c;
for(c=1; c<=2; c++) {
row = sheet.getRow(c);
long_sec = row.getCell(column_long_sec).getNumericCellValue();
longSec = Double.toString(long_sec);
}
Element sid_long_sec = document.createElement("SID_LONG_SEC");
document.appendChild(sid_long_sec);
sid_long_sec.appendChild(document.createTextNode(longSec));
The code give an output like this:
<SID_LONG_SEC>54.61</SID_LONG_SEC>
But, I want the code gives the output like this:
<SID_LONG_SEC>48.94</SID_LONG_SEC>
<SID_LONG_SEC>54.61</SID_LONG_SEC>
How to do that?
Any idea or help will be greatly appreciated
Best Regards,
Yunus
Upvotes: 1
Views: 220
Reputation: 1303
you´re creating the element outside the loop, so it only takes the last value of longSec.
this
Element sid_long_sec = document.createElement("SID_LONG_SEC");
document.appendChild(sid_long_sec);
sid_long_sec.appendChild(document.createTextNode(longSec));
should be inside the loop
Upvotes: 4
Reputation: 393771
How about doing it all inside the loop?
String longSec = null;
Double long_sec = null;
int column_long_sec = 33;
int c;
for(c=1; c<=2; c++) {
row = sheet.getRow(c);
long_sec = row.getCell(column_long_sec).getNumericCellValue();
longSec = Double.toString(long_sec);
Element sid_long_sec = document.createElement("SID_LONG_SEC");
document.appendChild(sid_long_sec);
sid_long_sec.appendChild(document.createTextNode(longSec));
}
If you want to leave the XML handling outside the loop, you have to store the values read from the excel file in some container (array or Collection) and then create another loop in which you'll iterate over that container and write the values to the XML.
Upvotes: 2