I've created a recursive function based on regex, in case you don't want to install library and understand the logic behind what's happening:
const xmlSample = '<tag>tag content</tag><tag2>another content</tag2><tag3><insideTag>inside content</insideTag><emptyTag /></tag3>';_x000D_
console.log(parseXmlToJson(xmlSample));_x000D_
_x000D_
function parseXmlToJson(xml) {_x000D_
const json = {};_x000D_
for (const res of xml.matchAll(/(?:<(\w*)(?:\s[^>]*)*>)((?:(?!<\1).)*)(?:<\/\1>)|<(\w*)(?:\s*)*\/>/gm)) {_x000D_
const key = res[1] || res[3];_x000D_
const value = res[2] && parseXmlToJson(res[2]);_x000D_
json[key] = ((value && Object.keys(value).length) ? value : res[2]) || null;_x000D_
_x000D_
}_x000D_
return json;_x000D_
}
_x000D_
Regex explanation for each loop:
<tag />
You can check how the regex works here: https://regex101.com/r/ZJpCAL/1
Note: In case json has a key with an undefined value, it is being removed. That's why I've inserted null at the end of line 9.