简体   繁体   English

XML 字符串到 JSON javascript

[英]XML string to JSON javascript

I have a xml string which i want to convert in JSON string我有一个 xml 字符串,我想将其转换为 JSON 字符串

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.我最初尝试使用解析器将其转换为 DOM 对象,但它引发了解析错误。

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

i want my output json string like only by using Javascript我希望我的输出 json 字符串就像只使用 Javascript

{"result":[{"id":"1","type":"HL","ven":"DEMOMA"},{"id":"2","type":"HL","ven":"DEMOMB"}]}

I will try to explain with an example with use x2js.js https://github.com/abdmob/x2js and jquery (and without jQuery) library.我将尝试用一个例子来解释使用x2js.js https://github.com/abdmob/x2jsjquery (没有 jQuery)库。

GET XML data from the API and convert this data to JSON从 API 获取 XML 数据并将此数据转换为 JSON

With jQuery使用 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没有 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>修复关闭的<result></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>

Check out this https://github.com/metatribal/xmlToJSON看看这个https://github.com/metatribal/xmlToJSON

Its a very small and useful script.它是一个非常小而有用的脚本。 Usage is very easy.使用非常简单。

Include the src包括 src

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

and enjoy!享受吧! xmlToJSON is packaged as a simple module, so use it like this xmlToJSON 被打包成一个简单的模块,所以像这样使用它

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

'result' is your JSON object. 'result' 是您的 JSON 对象。

I have a xml string which i want to convert in JSON string我有一个要转换为JSON字符串的xml字符串

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.我最初尝试使用解析器将其转换为DOM对象,但会引发解析错误。

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

i want my output json string like only by using Javascript我想要我的输出json字符串就像仅通过使用Javascript

{"result":[{"id":"1","type":"HL","ven":"DEMOMA"},{"id":"2","type":"HL","ven":"DEMOMB"}]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM