406 lines
17 KiB
JavaScript
406 lines
17 KiB
JavaScript
function json2xml() {
|
|
// console.clear();
|
|
var obj = localStorage.getItem("json");
|
|
|
|
// var obj = `{
|
|
// "question_paper_title": "<p><br></p>",
|
|
// "question_paper_description": "<p><br></p>",
|
|
// "max_marks": "",
|
|
// "course_ID": "",
|
|
// "course_Code": "",
|
|
// "subject_ID": "",
|
|
// "subject_Code": "",
|
|
// "paper_No": "",
|
|
// "subject_Name": "",
|
|
// "sub_Paper_Prefix": "",
|
|
// "question_Paper_uuid": "f676ad21-b0d2-4a80-8c77-6cd2b35f1b34",
|
|
// "sections": [
|
|
// {
|
|
// "section_title": "<p>0</p>",
|
|
// "section_description": "<p><br></p>",
|
|
// "section_marks": "0",
|
|
// "section_uuid": "7c62e594-3b93-444f-9fa6-a05f87869db6",
|
|
// "section_display_marks": true,
|
|
// "section_attempt_any": "",
|
|
// "questions": [
|
|
// {
|
|
// "question": "<p>1</p>",
|
|
// "question_description": "<p><br></p>",
|
|
// "question_type": "group",
|
|
// "question_sub_type": false,
|
|
// "is_math_enabled": false,
|
|
// "display_marks": true,
|
|
// "marks": "",
|
|
// "choice_question_uuid": "40f37294-e7cb-4f19-9166-72f7a04f6391",
|
|
// "answers": [
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "387d9b85-4702-4e77-a8c3-29d713b7689e",
|
|
// "no": "a"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "1a6be686-9e7c-4b07-bf15-b9e07a01fd62",
|
|
// "no": "b"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "0640c05b-522e-422c-8050-0d5982aa3c74",
|
|
// "no": "c"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "e59f90b0-79d5-413e-85a4-ee3c181230d8",
|
|
// "no": "d"
|
|
// }
|
|
// ],
|
|
// "correct_answer": ["1a6be686-9e7c-4b07-bf15-b9e07a01fd62"]
|
|
// },
|
|
// {
|
|
// "question": "<p>2</p>",
|
|
// "question_description": "<p><br></p>",
|
|
// "question_type": "group",
|
|
// "question_sub_type": false,
|
|
// "is_math_enabled": false,
|
|
// "display_marks": true,
|
|
// "marks": "",
|
|
// "choice_question_uuid": "7a1c83e7-e9ae-4424-9336-6135eaa6b833",
|
|
// "answers": [
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "0cbb8330-8ce9-4d69-b547-68cee529a636",
|
|
// "no": "a"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "25dc4fdc-7bcf-4bea-aaa5-dc42a22c08b5",
|
|
// "no": "b"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "5b0da327-076f-488c-a46e-0b8251c83283",
|
|
// "no": "c"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "d3934305-d6fd-4601-8b1b-08067f47a8dd",
|
|
// "no": "d"
|
|
// }
|
|
// ],
|
|
// "correct_answer": ["d3934305-d6fd-4601-8b1b-08067f47a8dd"]
|
|
// },
|
|
// {
|
|
// "question": "<p>3</p>",
|
|
// "question_description": "<p><br></p>",
|
|
// "question_type": "group",
|
|
// "question_sub_type": false,
|
|
// "is_math_enabled": false,
|
|
// "display_marks": true,
|
|
// "marks": "",
|
|
// "choice_question_uuid": "6f929b53-9ebc-40fb-800e-46c42b75f2d9",
|
|
// "answers": [
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "f839312c-8de5-4cd5-8d6c-3ffd3b9efea6",
|
|
// "no": "a"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "905db769-82b2-4240-af06-fe0b15ee1f73",
|
|
// "no": "b"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "6a16126f-8100-49f8-ae33-71d4848030cf",
|
|
// "no": "c"
|
|
// },
|
|
// {
|
|
// "choice_name": "<p><br></p>",
|
|
// "is_math_enabled": false,
|
|
// "choice_uuid": "143f648a-d52c-42ff-9428-1c080326e1c8",
|
|
// "no": "d"
|
|
// }
|
|
// ],
|
|
// "correct_answer": ["143f648a-d52c-42ff-9428-1c080326e1c8", "6a16126f-8100-49f8-ae33-71d4848030cf"]
|
|
// }
|
|
// ]
|
|
// }
|
|
// ]
|
|
// }
|
|
// `
|
|
// console.log(obj);
|
|
// console.log('***********************************************************');
|
|
|
|
function createXMLDocument(namespace = null) {
|
|
if (typeof document.implementation.createDocument === 'function') {
|
|
return document.implementation.createDocument(namespace, '', null);
|
|
} else if (typeof ActiveXObject !== 'undefined') {
|
|
// For Internet Explorer
|
|
var versions = ['MSXML2.DOMDocument.6.0', 'MSXML2.DOMDocument.3.0', 'Microsoft.XMLDOM'];
|
|
for (var i = 0; i < versions.length; i++) {
|
|
try {
|
|
var xmlDoc = new ActiveXObject(versions[i]);
|
|
return xmlDoc;
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
}
|
|
}
|
|
throw new Error('Your browser or environment does not support XML document creation.');
|
|
}
|
|
|
|
function createElementWithText(xmlDoc, parentElement, elementName, textContent, attributes = {}) {
|
|
var element = xmlDoc.createElement(elementName);
|
|
if (Object.keys(attributes).length != 0) {
|
|
for (const key in attributes) {
|
|
element.setAttribute(key, attributes[key])
|
|
}
|
|
|
|
}
|
|
element.appendChild(xmlDoc.createTextNode(textContent));
|
|
return parentElement.appendChild(element);
|
|
|
|
}
|
|
|
|
function createStandaloneXMLNode(tagName, attributes = {}, textContent = '') {
|
|
// console.log(attributes);
|
|
const xmlDoc = createXMLDocument();
|
|
const element = xmlDoc.createElement(tagName);
|
|
|
|
for (const attributeName in attributes) {
|
|
element.setAttribute(attributeName, attributes[attributeName]);
|
|
}
|
|
|
|
if (textContent !== '') {
|
|
const textNode = xmlDoc.createTextNode(textContent);
|
|
element.appendChild(textNode);
|
|
}
|
|
|
|
return {
|
|
'doc': xmlDoc,
|
|
'element': element
|
|
};
|
|
}
|
|
|
|
function getXMLNodesOfQuestionData(question) {
|
|
|
|
var {
|
|
doc,
|
|
element
|
|
} = createStandaloneXMLNode('Question', {
|
|
"xsi:type": (question['question_type'] == 'text' ? 'DescriptiveQuestion' : 'MultiChoiceQuestion'),
|
|
'Id': (question['question_type'] == 'text' ? question['text_question_uuid'] : question['choice_question_uuid']),
|
|
'type': (question['question_type'] == 'text' ? 'Descriptive' : 'Mcq')
|
|
|
|
}, '');
|
|
|
|
if (question['question_id'] !== undefined) {
|
|
var questionChoiceId =createElementWithText(doc, element, 'Text', question['question_id'])
|
|
} else {
|
|
// If question['question_id'] is undefined, create a new variable
|
|
var questionIdSet = createElementWithText(doc, element , 'Text', question['questionChoiceId'])
|
|
}
|
|
|
|
|
|
var questionTitle = createElementWithText(doc, element, 'Text', question['question']);
|
|
var questionDesc = createElementWithText(doc, element, 'Subtext', question['question_description']);
|
|
var marks = createElementWithText(doc, element, 'Marks', question['marks']);
|
|
var display_marks = createElementWithText(doc, element, 'DispalyMarks', (question['display_marks'] === true ? 'true' : 'false'), {});
|
|
var is_math_enabled = createElementWithText(doc, element, 'Math', (question['is_math_enabled'] === true ? 'true' : 'false'), {});
|
|
|
|
if (question['question_type'] == 'group') {
|
|
var hasMultipleAnswers = createElementWithText(doc, element, 'HasMultipleAnswers', (question['question_sub_type'] === true ? 'true' : 'false'), {});
|
|
var choicesElement = createElementWithText(doc, element, 'Choices', '', {});
|
|
for (choice in question['answers']) {
|
|
var choiceElement = createElementWithText(doc, choicesElement, 'Choice', '', {});
|
|
var id = createElementWithText(doc, choiceElement, 'Id', question['answers'][choice]['choice_uuid'], {});
|
|
var no = createElementWithText(doc, choiceElement, 'No', question['answers'][choice]['no'], {});
|
|
var text = createElementWithText(doc, choiceElement, 'Text', question['answers'][choice]['choice_name'], {});
|
|
var is_math_enabled = createElementWithText(doc, choiceElement, 'Math', (question['answers'][choice]['is_math_enabled'] === true ? 'true' : 'false'), {});
|
|
|
|
}
|
|
// choicesElement.appendChild();
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
function getXMLNodesOfGroupQuestionData(singleQuestion){
|
|
|
|
//create question group title related xml nodes
|
|
var {
|
|
doc,
|
|
element
|
|
} = createStandaloneXMLNode('Question', {
|
|
'xsi:type': 'QuestionGroup',
|
|
'Id': singleQuestion['group_uuid'],
|
|
'Type': 'QuestionGroup',
|
|
'No': singleQuestion['group_title'],
|
|
'Order': 0
|
|
}, '');
|
|
|
|
|
|
// var groupQuestionElement = createElementWithText(xmlDoc, sectionElement, 'Question', '', {
|
|
// 'xsi:type': 'QuestionGroup',
|
|
// 'Id': singleQuestion['group_uuid'],
|
|
// 'Type': 'QuestionGroup',
|
|
// 'No': singleQuestion['group_title'],
|
|
// 'Order': 0
|
|
// });
|
|
var groupQuestionId = createElementWithText(doc, element, 'Text', singleQuestion['group_id']);
|
|
var groupQuestionTitle = createElementWithText(doc, element, 'Text', singleQuestion['group_title']);
|
|
var groupQuestionDesc = createElementWithText(doc, element, 'Subtext', singleQuestion['group_description']);
|
|
var groupQuestionMarks = createElementWithText(doc, element, 'Marks', singleQuestion['group_marks']);
|
|
var groupQuestionDisplayMarks = createElementWithText(doc, element, 'DisplayMarks', (singleQuestion['group_display_marks'] === true ? 'true' : 'false' ));
|
|
var groupQuestionAttemptAny = createElementWithText(doc, element, 'AttemptAny', singleQuestion['group_attempt_any'] );
|
|
var groupQuestionItemsElement = createElementWithText(doc, element, 'Items', '');
|
|
|
|
//gather questions data inside question group
|
|
if (singleQuestion['questions'].length != 0) {
|
|
var tempGroupQuestions = singleQuestion['questions'];
|
|
//looping each questions indise question group and attach it to group items node
|
|
for (gquestion in tempGroupQuestions) {
|
|
var singleGQuestion = tempGroupQuestions[gquestion];
|
|
// console.log('singleGQuestion-'+gquestion);
|
|
// console.log(singleGQuestion);
|
|
// console.log('***********************************');
|
|
if (singleGQuestion.hasOwnProperty('question_type')) // normal questions inside group questions
|
|
{
|
|
console.log('Single Question');
|
|
var singleGQuestionElement = getXMLNodesOfQuestionData(singleGQuestion);
|
|
//attach into group items node
|
|
groupQuestionItemsElement.appendChild(singleGQuestionElement);
|
|
}
|
|
else
|
|
{
|
|
// console.log('Group Question');
|
|
var multiGQuestionElement = getXMLNodesOfGroupQuestionData(singleGQuestion);
|
|
groupQuestionItemsElement.appendChild(multiGQuestionElement);
|
|
}
|
|
}
|
|
}
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
//start of the busi logic
|
|
questionPaper = JSON.parse(obj);
|
|
// console.log(questionPaper);
|
|
var ns1 = 'http://www.w3.org/2001/XMLSchema';
|
|
var xmlDoc = createXMLDocument(namespace = ns1);
|
|
var rootElement = xmlDoc.createElement('QuestionPaper');
|
|
rootElement.setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');
|
|
rootElement.setAttribute('xmlns:xsd','http://www.w3.org/2001/XMLSchema');
|
|
rootElement.setAttribute('Id', questionPaper.question_Paper_uuid);
|
|
rootElement.setAttribute('MaxMarks',questionPaper.max_marks);
|
|
rootElement.setAttribute('CourseId',questionPaper.course_ID);
|
|
rootElement.setAttribute('CourseCode',questionPaper.course_Code);
|
|
rootElement.setAttribute('SubjectId',questionPaper.subject_ID);
|
|
rootElement.setAttribute('SubjectCode',questionPaper.subject_Code);
|
|
rootElement.setAttribute('PaperNo',questionPaper.paper_No);
|
|
rootElement.setAttribute('SubjectName',questionPaper.subject_Name);
|
|
rootElement.setAttribute('SubPaperPrefix',questionPaper.sub_Paper_Prefix);
|
|
xmlDoc.appendChild(rootElement);
|
|
|
|
|
|
createElementWithText(xmlDoc, rootElement, 'Title', questionPaper.question_paper_title);
|
|
createElementWithText(xmlDoc, rootElement, 'Subtitle', questionPaper.question_paper_description);
|
|
var rootItemsElement = createElementWithText(xmlDoc, rootElement, 'Items', '', );
|
|
|
|
//iterate each sections
|
|
for (section in questionPaper['sections']) {
|
|
//create section related xml nodes
|
|
var tempSection = questionPaper['sections'][section];
|
|
|
|
// console.log(tempSection);
|
|
|
|
var sectionElement = createElementWithText(xmlDoc, rootItemsElement, 'Question', '', {
|
|
'xsi:type': 'QuestionPaperSection',
|
|
'Id': tempSection['section_uuid'],
|
|
'Type': 'Section',
|
|
'No': tempSection['section_title'],
|
|
'Order': 0
|
|
});
|
|
|
|
var sectionTitle = createElementWithText(xmlDoc, sectionElement, 'Text', tempSection['section_title']);
|
|
var sectionDesc = createElementWithText(xmlDoc, sectionElement, 'Subtext', tempSection['section_description']);
|
|
var sectionMarks = createElementWithText(xmlDoc, sectionElement, 'Marks', tempSection['section_marks']);
|
|
var sectionDisplayMark = createElementWithText(xmlDoc, sectionElement, 'DisplayMarks', (tempSection['section_display_marks'] === true ? 'true' : 'false'));
|
|
var sectionAttempAny = createElementWithText(xmlDoc, sectionElement, 'AttemptAny', tempSection['section_attempt_any']);
|
|
var sectionItemElement = createElementWithText(xmlDoc, sectionElement, 'Items', '', );
|
|
|
|
|
|
//gather questions data
|
|
if (tempSection['questions'].length != 0) {
|
|
for (question in tempSection['questions']) {
|
|
var singleQuestion = tempSection['questions'][question];
|
|
|
|
|
|
//for Separate Correct Answer From MCQ
|
|
if(singleQuestion.question_type == 'group'){
|
|
|
|
separateCorrectAnswerFromMcqAsJson(singleQuestion)
|
|
}
|
|
|
|
|
|
if (singleQuestion.hasOwnProperty('question_type')) // normal questions
|
|
{
|
|
var singleQuestionElement = getXMLNodesOfQuestionData(singleQuestion);
|
|
//attach each single question into section item node
|
|
sectionItemElement.appendChild(singleQuestionElement);
|
|
} else // handle question group
|
|
{
|
|
var groupQuestionElement = getXMLNodesOfGroupQuestionData(singleQuestion);
|
|
sectionItemElement.appendChild(groupQuestionElement);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
rootItemsElement.appendChild(sectionElement);
|
|
}
|
|
rootElement.appendChild(rootItemsElement);
|
|
|
|
// Serialize the XML to a string
|
|
var xmlString = new XMLSerializer().serializeToString(xmlDoc);
|
|
|
|
// console.log(xmlString);
|
|
localStorage.setItem("xml", xmlString);
|
|
|
|
}
|
|
|
|
|
|
const outputJson = { questions: [] };
|
|
|
|
function separateCorrectAnswerFromMcqAsJson(tempSection) {
|
|
|
|
const questionId = tempSection.choice_question_uuid;
|
|
const answers = tempSection.correct_answer;
|
|
|
|
// Create an object for each question and push it to the output array
|
|
outputJson.questions.push({
|
|
questionId,
|
|
answers
|
|
});
|
|
|
|
// Convert the output object to JSON string
|
|
const outputJsonString = JSON.stringify(outputJson, null, 2);
|
|
localStorage.setItem("Correct_Answer_Json", outputJsonString);
|
|
console.log('outputJsonString : ', outputJsonString);
|
|
|
|
}
|
|
|