projectb/savexml.js
2023-10-07 17:35:29 +05:30

109 lines
2.5 KiB
JavaScript

const fs = require('fs');
const cheerio = require('cheerio');
const Swal = require('sweetalert2')
const encode = require('./encode')
require('dotenv').config();
const isEncrypted = process.env.ENCRYPTED;
function saveXmlToFile(xmlData, filePath) {
// Check if the file path is provided
if (!filePath) {
console.error('File path is required.');
return;
}
// Write the XML data to the specified file path
fs.writeFileSync(filePath, xmlData, 'utf8', (err) => {
console.log("fs write inside ");
alert("fs write inside")
if (err) {
console.error('Error writing XML file:', err);
} else {
console.log('XML file saved successfully.');
// Clear all data in local storage
localStorage.clear();
}
});
localStorage.clear();
window.location.href = 'dashboard.html';
}
function savexml(UserFilename) {
// Get the XML string from localStorage
var xmlString = localStorage.getItem("xml");
//check Encrypted or not
var enc = "";
if (isEncrypted === 'YES') {
// Encrypt
enc = encode(xmlString);
}else{
enc = xmlString;
}
// Get the json string from localStorage for set fileName
var jsonData = localStorage.getItem("json");
// Create a filename using on the subtitle
var xmlfilename = `${UserFilename}.xml`;
// var jsonfilename = `${UserFilename}.json`;
// file path where you want to save the XML file
const xmlfilePath = process.cwd() + '/xml/' + xmlfilename;
// const jsonfilePath = process.cwd() + '/xml/' + jsonfilename;
// Call the function to save the XML file
saveXmlToFile(enc, xmlfilePath);
}
function promptBox(){
var jsonData = localStorage.getItem("json");
var jsonString = JSON.parse(jsonData);
var getTitle = jsonString.question_paper_title;
var RawFileName = removeHtmlTags(getTitle);
console.log(RawFileName)
Swal.fire({
title: 'Enter Filename',
input: 'text',
inputValue: RawFileName,
showCancelButton: true,
confirmButtonText: 'Submit',
cancelButtonText: 'Cancel',
inputValidator: (value) => {
if (!value) {
return 'You need to enter Filename!'
}
}
}).then((result) => {
if (result.isConfirmed) {
const inputValue = result.value
savexml(inputValue)
}
})
}
// function using for REMOVE HTML ELEMENT in the String
function removeHtmlTags(inputString) {
const $ = cheerio.load(inputString);
const text = $.text();
return text;
}