|
1 |
| -//package javaToolkit.lib.utils; |
2 |
| -// |
3 |
| -//import java.io.File; |
4 |
| -//import javax.xml.transform.*; |
5 |
| -//import javax.xml.transform.dom.DOMSource; |
6 |
| -//import javax.xml.transform.stream.StreamResult; |
7 |
| -//import javax.xml.parsers.*; |
8 |
| -//import org.w3c.dom.Attr; |
9 |
| -//import org.w3c.dom.Document; |
10 |
| -//import org.w3c.dom.Element; |
11 |
| -// |
12 |
| -//import smu.bowen.ct4j.datapreparation.Group; |
13 |
| -//import smu.bowen.ct4j.datapreparation.Constants; |
14 |
| -// |
15 |
| -//public class XMLUtil { |
16 |
| -// |
17 |
| -// public static Boolean writeProjectGroup(Group group, String xmlPath) { |
18 |
| -// |
19 |
| -// try { |
20 |
| -// DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); |
21 |
| -// DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); |
22 |
| -// Document document = documentBuilder.newDocument(); |
23 |
| -// |
24 |
| -// // root element |
25 |
| -// Element root = document.createElement("list"); |
26 |
| -// document.appendChild(root); |
27 |
| -// |
28 |
| -// // projectGroup element |
29 |
| -// Element projectGroup = document.createElement("projectGroup"); |
30 |
| -// root.appendChild(projectGroup); |
31 |
| -// |
32 |
| -// // set an attribute to staff element |
33 |
| -// Attr groupAttr = document.createAttribute("id"); |
34 |
| -// groupAttr.setValue(String.valueOf(group.groupId)); |
35 |
| -// projectGroup.setAttributeNode(groupAttr); |
36 |
| -// |
37 |
| -// // bowen define the #train |
38 |
| -// System.out.println(group.methodList.size()); |
39 |
| -// if (group.methodList.size() < 10) { |
40 |
| -// return null; |
41 |
| -// } |
42 |
| -// int trainNum; |
43 |
| -// if ((int) (group.methodList.size() * Constants.trainRatio) > 40) { |
44 |
| -// trainNum = 40; |
45 |
| -// } else { |
46 |
| -// trainNum = (int) (group.methodList.size() * Constants.trainRatio); |
47 |
| -// } |
48 |
| -// |
49 |
| -// for (int i = 0; i < trainNum; i++) { |
50 |
| -// |
51 |
| -// System.out.println(group.methodList.get(i).oldProjectName); |
52 |
| -// |
53 |
| -// Method method = group.methodList.get(i); |
54 |
| -// method.id = i + 1; |
55 |
| -// // method element |
56 |
| -// Element projectMethod = document.createElement("projectMethod"); |
57 |
| -// projectGroup.appendChild(projectMethod); |
58 |
| -// |
59 |
| -// // set an attribute to staff element |
60 |
| -// Attr methodAttr = document.createAttribute("id"); |
61 |
| -// methodAttr.setValue(String.valueOf(method.id)); |
62 |
| -// projectMethod.setAttributeNode(methodAttr); |
63 |
| -// |
64 |
| -// // src element |
65 |
| -// Element src = document.createElement("src"); |
66 |
| -// src.appendChild(document.createTextNode("SOMETHING")); |
67 |
| -// projectMethod.appendChild(src); |
68 |
| -// |
69 |
| -// // oldProjectName element |
70 |
| -// Element oldProjectName = document.createElement("oldProjectName"); |
71 |
| -// oldProjectName.appendChild(document.createTextNode(method.oldProjectName)); |
72 |
| -// projectMethod.appendChild(oldProjectName); |
73 |
| -// |
74 |
| -// // newProjectName element |
75 |
| -// Element newProjectName = document.createElement("newProjectName"); |
76 |
| -// newProjectName.appendChild(document.createTextNode(method.newProjectName)); |
77 |
| -// projectMethod.appendChild(newProjectName); |
78 |
| -// |
79 |
| -// // oldClassName element |
80 |
| -// Element oldClassName = document.createElement("oldClassName"); |
81 |
| -// oldClassName.appendChild(document.createTextNode(method.oldClassName)); |
82 |
| -// projectMethod.appendChild(oldClassName); |
83 |
| -// |
84 |
| -// // newClassName element |
85 |
| -// Element newClassName = document.createElement("newClassName"); |
86 |
| -// newClassName.appendChild(document.createTextNode(method.newClassName)); |
87 |
| -// projectMethod.appendChild(newClassName); |
88 |
| -// |
89 |
| -// // oldFilePath element |
90 |
| -// Element oldFilePath = document.createElement("oldFilePath"); |
91 |
| -// oldFilePath.appendChild(document.createTextNode(method.oldFilePath)); |
92 |
| -// projectMethod.appendChild(oldFilePath); |
93 |
| -// |
94 |
| -// // newFilePath element |
95 |
| -// Element newFilePath = document.createElement("newFilePath"); |
96 |
| -// newFilePath.appendChild(document.createTextNode(method.newFilePath)); |
97 |
| -// projectMethod.appendChild(newFilePath); |
98 |
| -// |
99 |
| -// // oldMethodName element |
100 |
| -// Element oldMethodName = document.createElement("oldMethodName"); |
101 |
| -// oldMethodName.appendChild(document.createTextNode(method.oldMethodName)); |
102 |
| -// projectMethod.appendChild(oldMethodName); |
103 |
| -// |
104 |
| -// // newMethodName element |
105 |
| -// Element newMethodName = document.createElement("newMethodName"); |
106 |
| -// newMethodName.appendChild(document.createTextNode(method.newMethodName)); |
107 |
| -// projectMethod.appendChild(newMethodName); |
108 |
| -// } |
109 |
| -// |
110 |
| -// // candidate element |
111 |
| -// int candidateNum = 1; |
112 |
| -// Element candidateProjects = document.createElement("candidateProjects"); |
113 |
| -// projectGroup.appendChild(candidateProjects); |
114 |
| -// |
115 |
| -// for (int i = trainNum; i < group.methodList.size(); i++) { |
116 |
| -// |
117 |
| -// Method method = group.methodList.get(i); |
118 |
| -// |
119 |
| -// // newMethodName element |
120 |
| -// Element candidateProject = document.createElement("candidateProject"); |
121 |
| -// |
122 |
| -// // set an attribute to staff element |
123 |
| -// Attr candidateProjectAttr = document.createAttribute("id"); |
124 |
| -// candidateProjectAttr.setValue(String.valueOf(candidateNum)); |
125 |
| -// candidateProject.setAttributeNode(candidateProjectAttr); |
126 |
| -// |
127 |
| -// candidateNum++; |
128 |
| -// |
129 |
| -// candidateProject.appendChild(document.createTextNode(method.oldProjectName)); |
130 |
| -// candidateProjects.appendChild(candidateProject); |
131 |
| -// } |
132 |
| -// |
133 |
| -// // write the content into xml file |
134 |
| -// TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
135 |
| -// Transformer transformer = transformerFactory.newTransformer(); |
136 |
| -// transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
137 |
| -// transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
138 |
| -// DOMSource source = new DOMSource(document); |
139 |
| -// |
140 |
| -// // Output to console for testing |
141 |
| -// StreamResult resultConsole = new StreamResult(System.out); |
142 |
| -// transformer.transform(source, resultConsole); |
143 |
| -// |
144 |
| -// StreamResult resultFile = new StreamResult(new File(xmlPath)); |
145 |
| -// transformer.transform(source, resultFile); |
146 |
| -// System.out.println("File saved!"); |
147 |
| -// |
148 |
| -// } catch (ParserConfigurationException pce) { |
149 |
| -// pce.printStackTrace(); |
150 |
| -// } catch (TransformerException tfe) { |
151 |
| -// tfe.printStackTrace(); |
152 |
| -// } |
153 |
| -// |
154 |
| -// return true; |
155 |
| -// } |
156 |
| -// |
157 |
| -//} |
| 1 | +package javaToolkit.lib.utils; |
| 2 | + |
| 3 | +import java.io.StringWriter; |
| 4 | + |
| 5 | +import javax.xml.transform.*; |
| 6 | +import javax.xml.transform.dom.DOMSource; |
| 7 | +import javax.xml.transform.stream.StreamResult; |
| 8 | +import org.w3c.dom.Node; |
| 9 | + |
| 10 | +public class XMLUtil { |
| 11 | + |
| 12 | + public static String nodeToString(Node node) { |
| 13 | + StringWriter sw = new StringWriter(); |
| 14 | + try { |
| 15 | + Transformer t = TransformerFactory.newInstance().newTransformer(); |
| 16 | + t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
| 17 | + t.setOutputProperty(OutputKeys.INDENT, "yes"); |
| 18 | + t.transform(new DOMSource(node), new StreamResult(sw)); |
| 19 | + } catch (TransformerException te) { |
| 20 | + System.out.println("nodeToString Transformer Exception"); |
| 21 | + } |
| 22 | + return sw.toString(); |
| 23 | + } |
| 24 | + |
| 25 | +} |
0 commit comments