Angular2:将 XML 转换为 JSON [英] Angular2: Convert XML to JSON
本文介绍了Angular2:将 XML 转换为 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将我从 Web API 响应收到的 XML 转换为 Angular 2 中的 JSON.该应用程序是用 Nativescript 开发的.无法为此找到解决方案.
I wanted to convert the XML I received from Web API response to JSON in Angular 2. The application is developed in Nativescript. Not able to find a solution for this.
推荐答案
我发现了一个很棒的包,可以让这一切变得非常简单.
I found an amazing package to make this very simple.
对我来说,我是在 angular 2 应用程序中但在节点端进行的.
For me on I am doing it in an angular 2 application but on the node side.
npm install xml2js --save
字面意思就是像这样传递xml一样简单,
It is literally as simple as passing the xml like this,
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.dir(result);
});
在我的应用程序中,我有一个 xml 文件并像这样使用它,
In my app I had an xml file and used it like this,
var fs = require('fs');
var parseString = require('xml2js').parseString;
function requestCreditReport(callback) {
fs.readFile('./credit-api/response.xml', 'utf8', function (err,data) {
if (err) return callback(err);
parseString(data, callback);
});
}
我希望这会有所帮助.
这篇关于Angular2:将 XML 转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文