-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXML-JS.js
More file actions
132 lines (125 loc) · 3.33 KB
/
XML-JS.js
File metadata and controls
132 lines (125 loc) · 3.33 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const parser = new DOMParser();
const xmlString = `
<list>
<student>
<name lang="en">
<first>Ivan</first>
<second>Ivanov</second>
</name>
<age>35</age>
<prof>teacher</prof>
</student>
<student>
<name lang="ru">
<first>Петр</first>
<second>Петров</second>
</name>
<age>58</age>
<prof>driver</prof>
</student>
<student>
<name lang="en">
<first>John</first>
<second>Doe</second>
</name>
<age>40</age>
<prof>engineer</prof>
</student>
<student>
<name lang="ru">
<first>Анна</first>
<second>Смирнова</second>
</name>
<age>32</age>
<prof>doctor</prof>
</student>
<student>
<name lang="en">
<first>Sarah</first>
<second>Johnson</second>
</name>
<age>45</age>
<prof>lawyer</prof>
</student>
<student>
<name lang="ru">
<first>Михаил</first>
<second>Иванов</second>
</name>
<age>50</age>
<prof>manager</prof>
</student>
<student>
<name lang="en">
<first>Emily</first>
<second>Williams</second>
</name>
<age>28</age>
<prof>architect</prof>
</student>
<student>
<name lang="ru">
<first>Екатерина</first>
<second>Павлова</second>
</name>
<age>37</age>
<prof>accountant</prof>
</student>
<student>
<name lang="en">
<first>Michael</first>
<second>Brown</second>
</name>
<age>48</age>
<prof>scientist</prof>
</student>
<student>
<name lang="ru">
<first>Дмитрий</first>
<second>Кузнецов</second>
</name>
<age>42</age>
<prof>musician</prof>
</student>
<student>
<name lang="en">
<first>Lisa</first>
<second>Miller</second>
</name>
<age>30</age>
<prof>artist</prof>
</student>
<student>
<name lang="ru">
<first>Ольга</first>
<second>Алексеева</second>
</name>
<age>25</age>
<prof>designer</prof>
</student>
</list>
`;
// Парсинг XML
const xmlDOM = parser.parseFromString(xmlString, "text/xml");
// Функция для получения данных из одного студента
function getStudentData(studentNode) {
const nameNode = studentNode.querySelector("name");
const firstNode = studentNode.querySelector("name first");
const secondNode = studentNode.querySelector("name second");
const ageNode = studentNode.querySelector("age");
const profNode = studentNode.querySelector("prof");
return {
name: `${firstNode.textContent} ${secondNode.textContent}`,
age: ageNode.textContent,
prof: profNode.textContent,
lang: nameNode.getAttribute('lang')
};
}
// Собираем данные всех студентов
const students = xmlDOM.querySelectorAll("student");
const list = Array.from(students).map(getStudentData);
// Формируем итоговый объект
const result = {
list: list
};
console.log(result);