[HTML]
<script>
// pass your raw xml data here
var encodedStr = '<level3Response><messages><messagetext="Syncfusion"><correlationId>01710231451275401143</correlationId><messageId>20171023145-1275401143</messageId><responseTime>15ms</responseTime></message></messages></level3Response>';
//define the mapper field to be displayed as text in TreeView
var mapper = [];
//it will replace the tag name to custom attribute as TreeView node text
mapper.push({ TagName: "message", TextField: "text" });
convertTreeData(encodedStr, mapper);
//to convert raw xml to xml text node
function convertTreeData(encodedStr, mapper) {
window.treeData = [];
window.id = 1;
var parser = new DOMParser;
var dom = parser.parseFromString(
'<!doctype html><body>' + encodedStr,
'text/html');
var decodedString = dom.body.textContent;
var XmlData = parser.parseFromString(decodedString, "text/xml");
xmlToJson(XmlData, window.treeData, mapper);
}
//to convert xml to json with required format
function xmlToJson(args, treeData, mapper) {
for (var i = 0, length = args.childNodes.length; i < length; i++) {
var nodeText = undefined, tag = args.childNodes[i]
for (var j = 0; j < mapper.length; j++) {
if (mapper[j].TagName == tag.tagName) {
nodeText = tag.getAttribute(mapper[j].TextField);
break;
}
}
nodeText = (nodeText != undefined) ? nodeText : (tag.nodeType == 1 ? tag.tagName : tag.textContent);
var obj = { "NodeId": window.id, "NodeText": nodeText };
treeData.push(obj);
window.id++;
if (args.hasChildNodes()) {
if (obj['Nodechild'] == undefined) {
obj['Nodechild'] = [];
}
xmlToJson(tag, obj['Nodechild'], mapper);
if (obj["Nodechild"].length == 0) {
delete obj["Nodechild"];
}
}
}
}
</script> |
Hi Shaurya,
Greetings from Syncfusion support.
We have prepared a sample for dynamically bind the XML datasource to the treeview. We used a generic method to convert the unknown structure of XML data to JSON with the required format for TreeView.In the below example, we have bound the raw XML data into our EJ2 TreeView by using a generic method (convertTreeData). This method is used to convert raw XML data to required JSON of TreeView by provided the mapping field.
[HTML]
<script>// pass your raw xml data herevar encodedStr = '<level3Response><messages><messagetext="Syncfusion"><correlationId>01710231451275401143</correlationId><messageId>20171023145-1275401143</messageId><responseTime>15ms</responseTime></message></messages></level3Response>';//define the mapper field to be displayed as text in TreeViewvar mapper = [];//it will replace the tag name to custom attribute as TreeView node textmapper.push({ TagName: "message", TextField: "text" });convertTreeData(encodedStr, mapper);//to convert raw xml to xml text nodefunction convertTreeData(encodedStr, mapper) {window.treeData = [];window.id = 1;var parser = new DOMParser;var dom = parser.parseFromString('<!doctype html><body>' + encodedStr,'text/html');var decodedString = dom.body.textContent;var XmlData = parser.parseFromString(decodedString, "text/xml");xmlToJson(XmlData, window.treeData, mapper);}//to convert xml to json with required formatfunction xmlToJson(args, treeData, mapper) {for (var i = 0, length = args.childNodes.length; i < length; i++) {var nodeText = undefined, tag = args.childNodes[i]for (var j = 0; j < mapper.length; j++) {if (mapper[j].TagName == tag.tagName) {nodeText = tag.getAttribute(mapper[j].TextField);break;}}nodeText = (nodeText != undefined) ? nodeText : (tag.nodeType == 1 ? tag.tagName : tag.textContent);var obj = { "NodeId": window.id, "NodeText": nodeText };treeData.push(obj);window.id++;if (args.hasChildNodes()) {if (obj['Nodechild'] == undefined) {obj['Nodechild'] = [];}xmlToJson(tag, obj['Nodechild'], mapper);if (obj["Nodechild"].length == 0) {delete obj["Nodechild"];}}}}</script>
Sample:
Please check the sample and let us know if you have any concerns on this.
Thanks,
Christo
public onCreate()
{
var proxy = this;
const ajax = new Ajax('http://localhost/xmlData/myFile.xml', 'GET');
ajax.send();
ajax.onSuccess = (data: any) => {
var parseString = require('xml2js').parseString;
parseString(data, function(err, result) {
var dataSource = result.catalog.book;
proxy.tree.fields.dataSource = dataSource;
});
};
} |