const path = require('path'); const fs = require('fs'); const xml2js = require('xml2js'); $("#overlay").show(); document.addEventListener('DOMContentLoaded', async () => { function getFilesInFolder(folderName) { const xmlFilePath = path.join(__dirname, folderName); try { const fileNames = fs.readdirSync(xmlFilePath); const xmlFiles = fileNames.filter((file) => file.endsWith('.xml')); return xmlFiles; } catch (error) { console.error('Error reading folder:', error); return []; } } require('dotenv').config(); //XmlPath get from .env const xmlPath=process.env.XML_FILE_PATH; const folderName = `/${xmlPath}`; const filePaths = []; const files = getFilesInFolder(folderName); files.forEach((file) => { const filePath = file; filePaths.push(filePath); }); // Function to load XML data and populate the list view function loadList(xmlFilePath) { return fetch(`${xmlPath}`+xmlFilePath) .then(response => response.text()) .then(xmlContent => { const parser = new DOMParser(); const xmlDoc = parser.parseFromString(xmlContent, 'text/xml'); var titleText = $(xmlDoc).find('Title').text(); return titleText; }) .catch(error => { console.error("Error loading XML:", error); throw error; }); } // Function to load and display XML files in a loop async function loadMultipleXMLs() { for (const xmlFile of filePaths) { console.log(xmlFile) try { const titleText = await loadList(xmlFile); // Initially load in list view const dynamicId = `download-button-${Date.now()}`; // Handle list view const html_for_list = `
${xmlFile}
`; $('#list').append(html_for_list); // Add event listener for download buttons $('#'+dynamicId).on('click',function (event) { event.preventDefault(); const filename =$(this).data('filename'); downloadFile(filename); }); } catch (error) { console.error("Error loading XML:", error); } } } // Call the function to load and display XML files loadMultipleXMLs(); $("#overlay").hide(); });