I will try to explain with an example with use x2js.js
https://github.com/abdmob/x2js and jquery
(and without jQuery) library.
GET XML data from the API and convert this data to JSON
With jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
<script type="text/javascript" src="xml2json.js"></script>
</head>
<body>
<script type="text/javascript">
var x2js = new X2JS();
$.ajax({
url: 'http://ip-api.com/xml',
dataType: 'XML',
success: function(data) {
var xmlText = data; // XML
var jsonObj = x2js.xml2json(xmlText); // Convert XML to JSON
console.log(jsonObj);
}
});
</script>
</body>
</html>
without jQuery
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="xml2json.js"></script>
</head>
<body>
<script type="text/javascript">
function loadXMLDoc(dname) {
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET", dname, false);
xhttp.send();
return xhttp.responseXML;
}
var xmlDoc = loadXMLDoc("http://ip-api.com/xml"); // XML
var x2js = new X2JS();
var jsonObj = x2js.xml2json(xmlDoc); // Convert XML to JSON
console.log(jsonObj);
</script>
</body>
</html>
and using the example that you gave in question. Fix closed <result>
to </result>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="xml2json.js"></script>
</head>
<body>
<script type="text/javascript">
var txt = "<?xml version='1.0' encoding='UTF-8' ?> <result> <info> <id>1</id> <type>HL</type> <ven>DEMOMA</ven> </info> <info> <id>2</id> <type>HL</type> <ven>DEMOMB</ven> </info> </result>";
var x2js = new X2JS();
var jsonObj = x2js.xml_str2json(txt);
console.log(jsonObj);
</script>
</body>
</html>