-1

based on xml below:

<TEST>
<TEST2>
Sample text
</TEST2>
</TEST>

how can i replace <TEST2> with Empty.string or "". This is the expected output:

<TEST>

Sample text

</TEST>
Mana
  • 13
  • 1
  • 7

1 Answers1

0

Try this:

const { DOMParser } = require('xmldom')

const rawXmlData = `
<?xml version = "1.0">
<TEST>
<TEST2>
Sample text
</TEST2>
<TEST2>
Another Sample text
</TEST2>
</TEST>
`;

const domParser = new DOMParser();
const xmlDoc = domParser.parseFromString(rawXmlData,"text/xml");
const tagsToRemove = xmlDoc.getElementsByTagName('TEST2'); // 2 elements
xmlDoc.removeChild(tagsToRemove);
const tagsThatGotRemoved = xmlDoc.getElementsByTagName('TEST2');
console.log(tagsThatGotRemoved.length); // 0