
function  xml2json(xmlData) {
// console.clear();
console.log('xml to json converstion');
console.log(xmlData);

function getAttributesOfXMLNode(node)
{
	// Get attributes of the node (if any)
	 attributes = {};
  if (node.attributes.length > 0) {
    for (let i = 0; i < node.attributes.length; i++) {
      const attribute = node.attributes[i];
      attributes[attribute.nodeName] = attribute.nodeValue;
    }
  }

   return attributes;
}

function getQuestionsNodes(questionNode,type)
{
		// console.log(questionNode);
		var questionObj = {};
		if(type == 1)
		{
				questionObj['question'] = questionNode.childNodes[0].firstChild.nodeValue;
				questionObj['question_description'] = (questionNode.childNodes[1].firstChild !== null? questionNode.childNodes[1].firstChild.nodeValue : "");
				questionObj['question_type'] = 'text';
				questionObj['attributes'] = getAttributesOfXMLNode(questionNode);
				questionObj['question_sub_type'] = 'false';
				questionObj['is_math_enabled'] = questionNode.childNodes[3].firstChild.nodeValue;

		}
		else
		{
				questionObj['question'] = questionNode.childNodes[0].firstChild.nodeValue;
				questionObj['question_description'] = (questionNode.childNodes[1].firstChild !== null? questionNode.childNodes[1].firstChild.nodeValue : "");
				questionObj['question_type'] = 'group';
				questionObj['attributes'] = getAttributesOfXMLNode(questionNode);
				questionObj['question_sub_type'] = questionNode.childNodes[4].firstChild.nodeValue;
				questionObj['is_math_enabled'] = questionNode.childNodes[3].firstChild.nodeValue;
				questionObj['answers'] = [];

				var choiceList = questionNode.childNodes[5].childNodes;
				
				if(choiceList.length > 0)
				{
					for (let k = 0; k < choiceList.length ;k++) 
					{
						var tempChoiceobj = {};
						 tempChoiceobj['id'] = choiceList[k].childNodes[0].firstChild.nodeValue;
						 tempChoiceobj['choice_name'] = choiceList[k].childNodes[1].firstChild.nodeValue;
						 tempChoiceobj['choice_description'] = (choiceList[k].childNodes[2].firstChild !== null ? choiceList[k].childNodes[2].firstChild.nodeValue : "");
						 // console.log(tempChoiceobj);
						 questionObj['answers'].push(tempChoiceobj);

					}
				}
		}
		return questionObj;

}
		
function getQuestionGroupNodes(questionGroup)
{
	var questionGroupObj = {};
	
				questionGroupObj['group_title'] = questionGroup.childNodes[0].firstChild.nodeValue;
				questionGroupObj['group_description'] = (questionGroup.childNodes[1].firstChild !== null ? questionGroup.childNodes[1].firstChild.nodeValue : "");
				questionGroupObj['attributes'] = getAttributesOfXMLNode(questionGroup);
				questionGroupObj['questions'] = [];
				//handle questions
				var questionList = questionGroup.childNodes[3].childNodes;
				console.log('No of quetions in GQ:' + questionList.length);
				for(let q = 0; q < questionList.length ;q++)
				{
						
						var questionType = questionList[q].getAttribute("xsi:type");
						console.log(questionType);
						if(questionType == 'DescriptiveQuestion')
						{
								questionGroupObj['questions'].push(getQuestionsNodes(questionList[q],1));
						}
						else if(questionType == 'MultiChoiceQuestion')
						{
								questionGroupObj['questions'].push(getQuestionsNodes(questionList[q],2));
						}
						
				}
				return questionGroupObj;
}



//busi logic start
var xmlString = xmlData;
const parser = new DOMParser();

// Parse the XML string into an XML document
const xmlDoc = parser.parseFromString(xmlString, 'text/xml');
const jsonObj = {};

jsonObj['question_paper_title'] = xmlDoc.getElementsByTagName("Title")[0].childNodes[0].nodeValue;
jsonObj['question_paper_description'] = xmlDoc.getElementsByTagName("Subtitle")[0].childNodes[0].nodeValue;
//get attributes of root/question paper element
jsonObj['attributes'] = getAttributesOfXMLNode(xmlDoc.documentElement);
jsonObj['sections'] = [];


	sectionsList = xmlDoc.getElementsByTagName("Items")[0].childNodes;
	console.log(sectionsList);
	console.log('no of sections:'+sectionsList.length);
	for (let i = 0; i < sectionsList.length ;i++) {
		var tempObj = {};

    console.log('Section :' + i + '-' + sectionsList[i].childNodes[0].firstChild.nodeValue);
		tempObj['section_title'] = sectionsList[i].childNodes[0].firstChild.nodeValue;
		tempObj['section_description'] = sectionsList[i].childNodes[1].firstChild.nodeValue;
		tempObj['attributes'] = getAttributesOfXMLNode(sectionsList[i]);
		tempObj['questions'] = [];

		//handle questions
		var questionList = sectionsList[i].childNodes[3].childNodes;
		console.log('No of quetions:' + questionList.length);
		// console.log(questionList[2]);
		for(let j = 0; j < questionList.length ;j++)
		{
				//check weather quesiotn or question grup
				console.log('QUESTION :' + j );
				// console.log(questionList[j].getAttribute("xsi:type"));
				var questionType = questionList[j].getAttribute("xsi:type");
				console.log(questionType);
				if(questionType == 'DescriptiveQuestion')
				{
						tempObj['questions'].push(getQuestionsNodes(questionList[j],1));
				}
				else if(questionType == 'MultiChoiceQuestion')
				{
						tempObj['questions'].push(getQuestionsNodes(questionList[j],2));
				}
				else if(questionType == 'QuestionGroup')
				{
					tempObj['questions'].push(getQuestionGroupNodes(questionList[j]));
				}	
		}
    jsonObj['sections'].push(tempObj);
}

    var jsonData = JSON.stringify(jsonObj);
	console.log('Xml to Json Conversion ');
	console.log(jsonData);
	console.log(jsonObj);
	jsonToHtml(jsonObj);
}


