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"}]}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With