-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>
4
  • Do you want to flatten just the <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>)? Commented May 23, 2022 at 6:15
  • 1
    stackoverflow.com/questions/6794027/… Commented May 23, 2022 at 6:15
  • @MindSwipe just <TEST2> Commented May 23, 2022 at 6:17
  • It is better to use XSLT for such tasks. Commented May 23, 2022 at 16:19

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.