【问题标题】:Save JSON data in XML file using angular 2 and TypeScript (on HTML 5 application)使用 Angular 2 和 TypeScript 将 JSON 数据保存在 XML 文件中(在 HTML 5 应用程序上)
【发布时间】:2017-01-06 16:57:14
【问题描述】:

我有带有 HTML5 应用程序的 Angular 2 (TypeScript)。 我想将 REST 服务响应(JSON 格式)保存在 XML 文件中。 我使用 FileSaver 来保存文件。它正在处理文本文件,但不以 XML 格式保存。 我想将 JSON 数据转换为 XML 格式,然后保存到文件中。 谁能帮我实现这个。

【问题讨论】:

    标签: html angular typescript


    【解决方案1】:
    npm install xml2json --save
    

    然后

    var parser = require('xml2json');
    
    var xml = "<foo attr=\"value\">bar</foo>";
    console.log("input -> %s", xml)
    
    // xml to json 
    var json = parser.toJson(xml);
    console.log("to json -> %s", json);
    
    // json to xml 
    var xml = parser.toXml(json);
    console.log("back to xml -> %s", xml)
    

    【讨论】:

    • 安装 xml2json 时出现错误:如果未定义 npm_config_node_gyp (node "C:\Users\kl358d\.npmrepo\node_modules\npm\bin\node-gyp-bin\\..\ ..\node_modules\node-gyp\bin\node-gyp.js" 重建 ) 否则 (节点 "" 重建 ) gyp 错误!配置错误
    • 我如何在 angular2/angular4 中使用 xml2json ?我如何在我的组件中导入?
    猜你喜欢
    • 2018-06-09
    • 2018-11-03
    • 2016-05-17
    • 2017-02-13
    • 2018-07-06
    • 2017-12-30
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    相关资源
    最近更新 更多