-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmlTree.js
More file actions
100 lines (92 loc) · 3.3 KB
/
xmlTree.js
File metadata and controls
100 lines (92 loc) · 3.3 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
class XmlTree {
constructor() {}
/**
* Converts an XML document or element into a JSON object.
*
* @param {Node} xml - The XML node to convert.
* @returns {Object|string} The JSON representation of the XML node. If the node is a text node, returns the text content as a string.
*
* @private
*/
#xmlToJson(xml) {
let obj = {};
if (xml.nodeType === 1) { // element node
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for (let j = 0; j < xml.attributes.length; j++) {
const attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue; // add attributes to the object
}
}
} else if (xml.nodeType === 3) { // text node
obj = xml.nodeValue; // set the object to the text content
}
if (xml.hasChildNodes()) { // if the node has child nodes
for (let i = 0; i < xml.childNodes.length; i++) {
const item = xml.childNodes.item(i);
const nodeName = item.nodeName;
if (typeof (obj[nodeName]) === "undefined") {
obj[nodeName] = this.#xmlToJson(item); // recursively convert child nodes
} else {
if (typeof (obj[nodeName].push) === "undefined") {
const old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old); // convert existing node to an array
}
obj[nodeName].push(this.#xmlToJson(item)); // add new node to the array
}
}
}
return obj; // return the JSON object
}
#jsonToTreeData(json, parentKey = '') {
console.log('json:', json);
const treeData = [];
for (const key in json) {
if (json.hasOwnProperty(key)) {
const node = {
text: key,
children: [],
state: {
opened: true
}
};
if (typeof json[key] === 'object') {
node.children = this.#jsonToTreeData(json[key], key);
} else {
node.text += `: ${json[key]}`;
}
treeData.push(node);
}
}
return treeData;
}
#createTreeData(xml) {
const json = this.#xmlToJson(xml);
const treeData = this.#jsonToTreeData(json);
return treeData;
}
showTree(xmlText) {
const xmlParser = new DOMParser();
const xml = xmlParser.parseFromString(xmlText, "application/xml");
const parseError = xml.getElementsByTagName("parsererror");
if (parseError.length > 0) {
console.error("Error parsing XML");
return;
}
const treeData = this.#createTreeData(xml);
console.log('treeData:', treeData);
let tree = new Tree('#tree-container', {data : treeData});
// $('#tree-container').jstree({
// 'core': {'data': treeData},
// 'plugins': ['checkbox']
// });
}
getSelectedNodes() {
const selectedNodes = $('#tree-container').jstree("get_selected", true);
const selectedData = selectedNodes.map(node => node.text);
return selectedData;
}
}
// Export the XmlTree class
// export default XmlTree;