- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriter.java
More file actions
Latest commit
93 lines (71 loc) · 3.72 KB
/
Copy pathWriter.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
importorg.w3c.dom.Document;
importorg.w3c.dom.Element;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.transform.Transformer;
importjavax.xml.transform.TransformerFactory;
importjavax.xml.transform.dom.DOMSource;
importjavax.xml.transform.stream.StreamResult;
importjava.io.File;
importjava.util.ArrayList;
publicclassWriter {
// Выставляем атрибуты для корневого элемента
publicvoidsetAttributes(ElementrootElement){
rootElement.setAttribute("category", "WEB");
rootElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
rootElement.setAttribute("xsi:noNamespaceSchemaLocation", "musicstore.xsd");
}
// Создаём инструменты при помощи "Фабрики"
publicvoidcreateInstruments(Documentdoc, Elementstore, Storestype) {
//Паттерн фабрика https://javarush.ru/groups/posts/2370-pattern-proektirovanija-factory
InstrumentFabricfabric = newInstrumentFabric();
ArrayList<Instrument> instruments = fabric.create(type);
for (Instrumentsingle_instrument : instruments){
Elementinstrument = 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);
}
}
// Создаём магазины
publicElementcreateStore(Documentdoc, ElementrootElement, Stringname){
Elementstore = doc.createElement("store");
store.setAttribute("name", name);
rootElement.appendChild(store);
returnstore;
}
publicvoidcreate(Stringname) {
try {
DocumentBuilderFactorydocFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderdocBuilder = docFactory.newDocumentBuilder();
//Создаём документ
Documentdoc = docBuilder.newDocument();
//Создаём основной элемент
ElementrootElement = doc.createElement("musictores");
setAttributes(rootElement);
doc.appendChild(rootElement);
Elementtitle = doc.createElement("title");
title.setAttribute("lang", "ru");
title.setTextContent("Магазин музыкальных инструментов");
rootElement.appendChild(title);
//Создаём дочерние элементы, магазины
Elementstore = 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 файл
TransformerFactorytransformerFactory = TransformerFactory.newInstance();
Transformertransformer = transformerFactory.newTransformer();
DOMSourcesource = newDOMSource(doc);
StreamResultresult = newStreamResult(newFile("C:\\Users\\justacold\\IdeaProjects\\SITAIRIS3\\" + name));
transformer.transform(source, result);
System.out.println("Done");
} catch (Exceptione) {
System.out.println("[Ошибка!] " + e.getMessage());
}
}
}