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>
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