Option Model Converter
by sberube
HTML
<div id="jsondata"></div>
JavaScript
var url = 'https://dl.dropboxusercontent.com/u/23785760/design.xml';
var myJsonData = {};
$.ajax({
type: 'GET',
url: url,
async: true,
contentType: "application/text",
dataType: 'text',
success: function (data) {
//alert('success');
//console.log(data);
xmlDoc = $.parseXML(data),
$xml = $(xmlDoc),
$optionModel = $xml.find("optionModel");
var result = {
'@self': '/csa/api/option-model/' + $optionModel.find('optionModel>id').text(),
'option_sets': []
};
var $optionSets = $optionModel.find('optionSets');
$optionSets.each(function (item) {
var $optionSet = $(this);
result['option_sets'].push(processOptionSet($optionSet));
});
$('#jsondata').text(JSON.stringify(result));
},
error: function (e) {
alert('error');
console.log(e);
}
});
var processOptionSet = function ($optionSet) {
var result = {};
result['@self'] = '/csa/api/option-model/option-set/' + $optionSet.find('optionSets>id').text();
result['name'] = $optionSet.find('optionSets>displayName').text();
result['description'] = $optionSet.find('optionSets>description').text();
result['icon'] = $optionSet.find('optionSets>iconUrl').text();
result.ext = {
csa_name_key: $optionSet.find('optionSets>name').text()
};
var minOccurs = $optionSet.find('optionSets>minOccurs').text();
var maxOccurs = $optionSet.find('optionSets>maxOccurs').text();
result['multi_select'] = (maxOccurs !== "1");
result['initial_order_only'] = ($optionSet.find('optionSets>isInitialOrder').text() === true ? true : false);
result['options'] = [];
var $options = $optionSet.find('optionSets>options');
$options.each(function (item) {
var $option = $(this);
result['options'].push(processOption($option));
});
return result;
};
var processOption = function ($option) {
...