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>
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
<TEST2>or do you want to flatten a whole document (i.e you have<TEST3>inside<TEST2>and<TEST4>being a sibling of<TEST2>, you'd end up with just<TEST>)?