- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderDOM.java
More file actions
Latest commit
61 lines (44 loc) · 1.93 KB
/
Copy pathReaderDOM.java
File metadata and controls
61 lines (44 loc) · 1.93 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
importorg.w3c.dom.*;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjava.io.File;
importjava.util.ArrayList;
publicclassReaderDOM {
Stringpath;
ArrayList<MusicStore> musicStores = newArrayList<>();
publicReaderDOM(Stringpath) {
this.path = path;
read();
}
privatevoidread() {
try {
FilefXmlFile = newFile(this.path);
DocumentBuilderFactorydbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilderdBuilder = dbFactory.newDocumentBuilder();
Documentdoc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeListstores = doc.getElementsByTagName("store");
for (inti = 0; i < stores.getLength(); i++) {
Elementstore = (Element) stores.item(i);
Stringname = store.getAttribute("name");
MusicStorenewStore = newMusicStore(name);
NodeListinstruments = store.getElementsByTagName("instrument");
for (intj = 0; j < instruments.getLength(); j++) {
Nodeinstrument = instruments.item(j);
NamedNodeMapattributes = instrument.getAttributes();
Instrumentnewinst = newInstrument(attributes.getNamedItem("class").getNodeValue(), attributes.getNamedItem("type").getNodeValue(), attributes.getNamedItem("firm").getNodeValue(), attributes.getNamedItem("name").getNodeValue());
newStore.add_instrum(newinst);
}
musicStores.add(newStore);
}
System.out.println("Успешно прочитали файл!");
} catch (Exceptione) {
System.out.println("[Ошибка!] " + e.getMessage());
}
}
publicvoidprint(){
for(MusicStorestore : musicStores){
store.print();
}
}
}