-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriter.java
More file actions
93 lines (71 loc) · 3.72 KB
/
Writer.java
File metadata and controls
93 lines (71 loc) · 3.72 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.util.ArrayList;
public class Writer {
// Выставляем атрибуты для корневого элемента
public void setAttributes(Element rootElement){
rootElement.setAttribute("category", "WEB");
rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootElement.setAttribute("xsi:noNamespaceSchemaLocation", "musicstore.xsd");
}
// Создаём инструменты при помощи "Фабрики"
public void createInstruments(Document doc, Element store, Stores type) {
//Паттерн фабрика https://javarush.ru/groups/posts/2370-pattern-proektirovanija-factory
InstrumentFabric fabric = new InstrumentFabric();
ArrayList<Instrument> instruments = fabric.create(type);
for (Instrument single_instrument : instruments){
Element instrument = doc.createElement("instrument");
instrument.setAttribute("class", single_instrument.m_class);
instrument.setAttribute("type", single_instrument.type);
instrument.setAttribute("firm", single_instrument.firm);
instrument.setAttribute("name", single_instrument.name);
store.appendChild(instrument);
}
}
// Создаём магазины
public Element createStore(Document doc, Element rootElement, String name){
Element store = doc.createElement("store");
store.setAttribute("name", name);
rootElement.appendChild(store);
return store;
}
public void create(String name) {
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
//Создаём документ
Document doc = docBuilder.newDocument();
//Создаём основной элемент
Element rootElement = doc.createElement("musictores");
setAttributes(rootElement);
doc.appendChild(rootElement);
Element title = doc.createElement("title");
title.setAttribute("lang", "ru");
title.setTextContent("Магазин музыкальных инструментов");
rootElement.appendChild(title);
//Создаём дочерние элементы, магазины
Element store = createStore(doc, rootElement, "Музыка");
createInstruments(doc, store, Stores.Music);
store = createStore(doc, rootElement, "Мелодия");
createInstruments(doc, store, Stores.Melody);
store = createStore(doc, rootElement, "Мир музыки");
createInstruments(doc, store, Stores.MusicWorld);
//Записываем то, что у нас получилось в xml файл
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\\Users\\justacold\\IdeaProjects\\SITAIRIS3\\" + name));
transformer.transform(source, result);
System.out.println("Done");
} catch (Exception e) {
System.out.println("[Ошибка!] " + e.getMessage());
}
}
}