1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
| DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new ParseException("Error while creating DocumentBuilder.", e);
}
Document document = builder.newDocument();
Node departmentRoot = document.createElement("department");
document.appendChild(departmentRoot);
Element deanElement = document.createElement("dean");
deanElement.setAttribute("name", department.getDean().getName());
deanElement.setAttribute("since", String.valueOf(department.getDean().getSince()));
departmentRoot.appendChild(deanElement);
Element studentsElement = document.createElement("students");
departmentRoot.appendChild(studentsElement);
Map<Student, List<Group>> studentGroups = new HashMap<Student, List<Group>>();
for (Group group : department.getGroups()) {
for (Student student : group.getStudents()) {
if (!studentGroups.containsKey(student)) {
studentGroups.put(student, new ArrayList<Group>());
}
studentGroups.get(student).add(group);
}
}
for (Map.Entry<Student, List<Group>> studentEntry : studentGroups.entrySet()) {
Element studentElement = document.createElement("student");
studentElement.setAttribute("name", studentEntry.getKey().getName());
studentElement.setAttribute("excellent", String.valueOf(studentEntry.getKey().isExcellent()));
studentsElement.appendChild(studentElement);
Element groupsElement = document.createElement("groups");
studentElement.appendChild(groupsElement);
for (Group group : studentEntry.getValue()) {
Node groupElement = document.createElement("group");
//
groupElement.setTextContent(String.valueOf(group.getNumber()));
groupsElement.appendChild(groupElement);
}
}
// вывод отформатированного xml
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = tFactory.newTransformer();
} catch (TransformerConfigurationException e) {
throw new ParseException("Error while creating DOM transformer", e);
}
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(file);
try {
transformer.transform(source, result);
} catch (TransformerException e) {
throw new ParseException("Error while transforming DOM document", e);
}
}
|