I need some help. I am checking the input string is json/xml using Node.js. I am explaining my code which gives the output to check the given string is json or not.
isJSON = (str) => {
try {
return (JSON.parse(str) && !!str);
} catch (e) {
return false;
}
}
const result= isJSON(string);
So here I am able to check whether the string i.e string is JSON or not. In similar way I need to check for XML content.
Lets say I have the string which is given below:
const string = '<config xmlns="http://tail-f.com/ns/config/1.0">
<devices xmlns="http://tail-f.com/ns/ncs">
<global-settings>
<read-timeout>120</read-timeout>
</global-settings>
</devices>
</config>';
So I need to check the above content is XML content or not using node.js. Can anybody help me to resolve this?