15

I have a xml string which i want to convert in JSON string

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

i tried to initially convert it in DOM object using parser but it throws parsing error.

parser = new DOMParser();
xmlDoc = parser.parseFromString(txt,"text/xml");

i want my output json string like only by using Javascript

{"result":[{"id":"1","type":"HL","ven":"DEMOMA"},{"id":"2","type":"HL","ven":"DEMOMB"}]}
5
  • 3
    Possible duplicate of Convert XML to JSON (and back) using Javascript Commented Apr 29, 2016 at 4:24
  • i can only use Javascript. No java no Jquery :( Commented Apr 29, 2016 at 4:29
  • jQuery is JavaScript Commented Apr 29, 2016 at 4:30
  • 2
    Your parsing problem is that you forgot the / in closing </result> closing tag. Commented Apr 29, 2016 at 4:48
  • try this jsfiddle.net/abdmob/gkxucxrj/1 Commented Apr 29, 2016 at 5:31

2 Answers 2

5

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

Comments

3

Check out this https://github.com/metatribal/xmlToJSON

Its a very small and useful script. Usage is very easy.

Include the src

<script type="text/javascript" src="path/xmlToJSON.js"></script>

and enjoy! xmlToJSON is packaged as a simple module, so use it like this

testString = '<xml><a>It Works!</a></xml>';   // get some xml (string or document/node)
result = xmlToJSON.parseString(testString);   // parse

'result' is your JSON object.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.