-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderDOM.java
More file actions
61 lines (44 loc) · 1.93 KB
/
ReaderDOM.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
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
public class ReaderDOM {
String path;
ArrayList<MusicStore> musicStores = new ArrayList<>();
public ReaderDOM(String path) {
this.path = path;
read();
}
private void read() {
try {
File fXmlFile = new File(this.path);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList stores = doc.getElementsByTagName("store");
for (int i = 0; i < stores.getLength(); i++) {
Element store = (Element) stores.item(i);
String name = store.getAttribute("name");
MusicStore newStore = new MusicStore(name);
NodeList instruments = store.getElementsByTagName("instrument");
for (int j = 0; j < instruments.getLength(); j++) {
Node instrument = instruments.item(j);
NamedNodeMap attributes = instrument.getAttributes();
Instrument newinst = new Instrument(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 (Exception e) {
System.out.println("[Ошибка!] " + e.getMessage());
}
}
public void print(){
for(MusicStore store : musicStores){
store.print();
}
}
}