Eksempel
Hvilket virker helt fint, mit spørgsmål er hvordan ville i eventuelt optimere følgende kode? Enten så den er mindre resource krævende, færre linjer / andet.Root element name: eveapi
namespace: []
date'\2016-11-05 15:08:16'\
refID'\13216563440'\
refTypeID'\1'\
ownerName1'\Benjamin Ambaek'\
ownerID1'\95654255'\
ownerName2'\Cristian Ambaek'\
ownerID2'\91472837'\
argName1'\Esa VIII - Moon 2 - Viziam Warehouse'\
argID1'\60006610'\
amount'\0.00'\
balance'\19691753.18'\
reason'\'\
taxReceiverID'\'\
taxAmount'\'\
owner1TypeID'\1385'\
owner2TypeID'\1377'\
Kode: Vælg alt
/**
* @param list
* XML document from getRootElement().getChildren().iterator()
* @param columnNames
* column names from column="", can be null
* @param compactArray
* array that will be used to build the output, can be null.
* @param arrayCount
* place data in the correct columns after adding the column
* name. Default start at 1.
*/
private static void diXmlRecursiveRead(Iterator<Element> list, String[] columnNames, String[][] compactArray,
Integer arrayCount) {
Element element = null;
String[] columns = null;
while (list.hasNext()) {
element = list.next();
// Assign data to the two dimensional array, starting from position
// 1 in the length to not overwrite the column names.
// This is also where i would print the column name plus its
// attribute value if i was printing to console.
if (columnNames != null) {
for (int a = 0; a < columnNames.length; a++) {
compactArray[a][arrayCount] = element.getAttributeValue(columnNames[a]);
}
}
// Find the element that contains the columns="" information
if (element.getAttributeValue("columns") != null) {
// reset the array count, since we are processing a new
// columns="" section
arrayCount = 1;
columns = element.getAttributeValue("columns").toString().split(",");
// set width size +1 to make room for the column names
// (columnNames + data +1)
compactArray = new String[columns.length][element.getChildren().size() + 1];
// Set the EVE column names in the first column of the two
// dimensional array
for (int a = 0; a < columns.length; a++) {
compactArray[a][0] = columns[a];
}
}
// After added the last value to the two dimensional array print the
// array[][]
if (compactArray != null) {
if (arrayCount == (compactArray[0].length - 1)) {
diPrintTwoDimensionalArrayCompact(compactArray);
}
}
// Method calls itself with a new level of the child
diXmlRecursiveRead(element.getChildren().iterator(), columns, compactArray, arrayCount++);
}
}
private static void diPrintTwoDimensionalArrayCompact(String[][] dataArray) {
StringBuilder sb = new StringBuilder();
String newline = System.getProperty("line.separator");
for (int a = 0; a < dataArray.length; a++) {
for (int b = 0; b < dataArray[0].length; b++) {
sb.append(dataArray[a][b] + "'\\");
}
sb.append(newline);
}
System.out.println(sb.toString());
}