I am loading a txt file and obtaining the values f...
# suitescript
m
I am loading a txt file and obtaining the values from the 'source' tags and trying to replace the 'destination tags'. Example, the source lines should replace the destination lines but it's not setting the destination lines correctly. Source <AddtlRmtInf>Hello 1</AddtlRmtInf> <AddtlRmtInf>Hello 2</AddtlRmtInf> <AddtlRmtInf>Hello 3</AddtlRmtInf> Destination <AddtlNtryInf>Hello 2</AddtlNtryInf> <AddtlNtryInf>Hello 3</AddtlNtryInf> <AddtlNtryInf>Test 3</AddtlNtryInf>
Copy code
const sourceTagStart = '<AddtlRmtInf>';
        const sourceTagEnd = '</AddtlRmtInf>';
        const destinationTagStart = '<AddtlNtryInf>';
        const destinationTagEnd = '</AddtlNtryInf>';

        // Extract values between source tags
        const tagValues = [];
        let startIndex = fileContent.indexOf(sourceTagStart);
        let endIndex;
        while (startIndex !== -1) {
            endIndex = fileContent.indexOf(sourceTagEnd, startIndex + sourceTagStart.length);
            if (endIndex === -1) {
                log.error({title: 'End tag not found', details: 'The end tag was not found in the file content.'});
                return;
            }
            tagValues.push(fileContent.substring(startIndex + sourceTagStart.length, endIndex));
            startIndex = fileContent.indexOf(sourceTagStart, endIndex + sourceTagEnd.length);
        }
        log.debug({title: 'Tag Values', details: tagValues}); //
[
   "Hello 1",
   "Hello 2",
   "Hello 3"
]

        // Replace destination tags with values
        startIndex = fileContent.indexOf(destinationTagStart);
        let tagIndex = 0;
        while (startIndex !== -1 && tagIndex < tagValues.length) {
            endIndex = fileContent.indexOf(destinationTagEnd, startIndex + destinationTagStart.length);
            if (endIndex === -1) {
                log.error({title: 'End tag not found', details: 'The end tag was not found in the file content.'});
                return;
            }
            fileContent = fileContent.substring(0, startIndex + destinationTagStart.length) +
                tagValues[tagIndex] +
                fileContent.substring(endIndex);
            tagIndex++;
            startIndex = fileContent.indexOf(destinationTagStart, endIndex + destinationTagEnd.length);
        }

        // Save the updated file
        const updatedFile = file.create({
            name: myFile.name,
            fileType: myFile.fileType,
            contents: fileContent,
            folder: myFile.folder
        });
        const updatedFileId = updatedFile.save();
        log.debug({title: 'Updated File ID', details: updatedFileId});
e
Seems like a good opportunity to leverage N/xml so you don't have to reinvent the wheel of parsing/generating xml and can instead focus on your business logic
I might also suggest a refactor such that you parse all the data you care about out of xml into primitive js values, transform all the js values as appropriate, then convert all the results back to xml - as opposed to looping through and running all of these steps on each value one at a time
👍 1
will work up an example of what i mean
❤️ 1
s
we reach for JSONIX for this.
1