15
15
import java .io .FileOutputStream ;
16
16
import java .util .ArrayList ;
17
17
18
- public class Util {
18
+ public class TabularToXMLConverter {
19
19
20
- /*/
21
- DOC Generation -> XML with ArrayList String elements
22
- */
23
- public Document docBuilder (ArrayList <String []> elements ) throws ParserConfigurationException {
20
+ //DOC Generation -> XML with ArrayList String elements
21
+ public Document docBuilder (ArrayList <String []> XMLelements , String elementName ) throws ParserConfigurationException {
24
22
DocumentBuilderFactory xmlFactory = DocumentBuilderFactory .newInstance ();
25
23
DocumentBuilder xmlBuilder = xmlFactory .newDocumentBuilder ();
26
24
Document xmlDoc = xmlBuilder .newDocument ();
27
25
28
26
Element rootElement = xmlDoc .createElement ("root" );
29
27
xmlDoc .appendChild (rootElement );
30
28
31
- Element mainElement = xmlDoc .createElement ("elements " );
29
+ Element mainElement = xmlDoc .createElement (elementName + "s " );
32
30
rootElement .appendChild (mainElement );
33
31
34
32
boolean headerDefined = false ; //First while will be to define header
35
- String [] header = new String [elements .size ()]; //Header initialization
33
+ String [] header = new String [XMLelements .size ()]; //Header initialization
36
34
37
- /*/
38
- DOC Generation -> XML Generation of every ELEMENT
39
- */
40
- for (String [] node : elements ) { //FOR every ArrayString
35
+ //DOC Generation -> XML Generation of every ELEMENT
36
+ for (String [] node : XMLelements ) { //FOR every ArrayString
41
37
if (headerDefined ) {
42
- Element nodesElements = xmlDoc .createElement ("element" );
38
+ Element nodesElements = xmlDoc .createElement (elementName );
43
39
mainElement .appendChild (nodesElements );
44
40
45
41
for (int j = 0 ; j < node .length ; j ++) {
@@ -50,17 +46,15 @@ public Document docBuilder(ArrayList<String[]> elements) throws ParserConfigurat
50
46
nodesValues .appendChild (nodeTxt );
51
47
}
52
48
}
53
- /*/
54
- DOC Generation -> Array Generation of every COL Name for NODES
55
- */
49
+ //DOC Generation -> Array Generation of every COL Name for NODES
56
50
else {
57
51
header = node ;
58
- for (int k = 0 ; k < node .length ; k ++) {
59
- header [k ] = header [k ].replaceAll ("[^a-zA-Z0-9]" , "" );
60
- //We want to make sure NODE isn't NUMERIC. If it is, we make a patch
52
+ for (int j = 0 ; j < node .length ; j ++) {
53
+ header [j ] = header [j ].replaceAll ("[^a-zA-Z0-9]" , "" );
54
+ //Avoid a fullint
61
55
try {
62
- Integer .parseInt (header [k ]);
63
- header [k ] = "node" + header [k ];
56
+ Integer .parseInt (header [j ]);
57
+ header [j ] = "node" + header [j ];
64
58
} catch (NumberFormatException e ) {
65
59
}
66
60
}
@@ -70,9 +64,7 @@ public Document docBuilder(ArrayList<String[]> elements) throws ParserConfigurat
70
64
return (xmlDoc );
71
65
}
72
66
73
- /*/
74
- XML Generation -> Transform DOC Data to XML Format
75
- */
67
+ //XML Generation -> Transform DOC Data to XML Format
76
68
public static void transformDocToFile (Document xmlDoc , String xmlFile ) throws TransformerException {
77
69
TransformerFactory xmlTransformerFactory = TransformerFactory .newInstance ();
78
70
Transformer xmlTransformer = xmlTransformerFactory .newTransformer ();
0 commit comments