angularjs中怎样将xml字符串转化为xml文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了angularjs中怎样将xml字符串转化为xml文件相关的知识,希望对你有一定的参考价值。

使用代码
首先创建一个html页面。

<!DOCTYPE html>
<html>
<head>
<title>Convert XML to JSON In Angular JS - SibeeshPassion </title>
</head>
<body>
</body>
</html>

现在,如下添加所需的引用。

<script src="jquery-2.1.3.min.js"></script>
<script src="angular.min.js"></script>
<script src="xml2json.js"></script>

你注意到了吗,我已经添加了xml2json.js文件?这就是将要转换的文件。你可以从 .google.com/p/x2js/下载文件。
现在创建一个控制器和应用程序指令,如下所示。

<div ng-app="httpApp" ng-controller="httpController">
</div>

接下来我们需要做的是添加服务。你可以按如下方式添加$http服务。

var app = angular.module(\'httpApp\', []);
app.controller(\'httpController\', function ($scope, $http)
$http.get("Sitemap.xml",

transformResponse: function (cnv)
var x2js = new X2JS();
var aftCnv = x2js.xml_str2json(cnv);
return aftCnv;

)
.success(function (response)
console.log(response);
);
);
参考技术A 使用代码
首先创建一个HTML页面。
<!DOCTYPE html>
<html>
<head>
<title>Convert XML to JSON In Angular JS - SibeeshPassion </title>
</head>
<body>
</body>
</html>

现在,如下添加所需的引用。
<script src="jquery-2.1.3.min.js"></script>
<script src="angular.min.js"></script>
<script src="xml2json.js"></script>

你注意到了吗,我已经添加了xml2json.js文件?这就是将要转换的文件。你可以从https://code.google.com/p/x2js/下载文件。
现在创建一个控制器和应用程序指令,如下所示。
<div ng-app="httpApp" ng-controller="httpController">
</div>

接下来我们需要做的是添加服务。你可以按如下方式添加$http服务。
var app = angular.module('httpApp', []);
app.controller('httpController', function ($scope, $http)
$http.get("Sitemap.xml",

transformResponse: function (cnv)
var x2js = new X2JS();
var aftCnv = x2js.xml_str2json(cnv);
return aftCnv;

)
.success(function (response)
console.log(response);
);
);

这里的httpApp是我们的应用程序,httpController是我们的控制器。我们使用函数transformResponse转换响应。
转换请求和响应
在Angular JS中,请求可以使用transformRequest函数转换,如果是响应的话,可以通过函数transformResponse转换。这些函数返回转换后的值。
下面是我们使用transformResponse函数的一个例子。
transformResponse: function (cnv)
var x2js = new X2JS();
var aftCnv = x2js.xml_str2json(cnv);
return aftCnv;


这个x2js.xml_str2json(CNV)将返回JSON对象,并从transformResponse函数返回aftCnv。听起来不错吧?一旦准备就绪,我们只要在浏览器控制台写JSON对象,就可以看到该对象了。

以上是关于angularjs中怎样将xml字符串转化为xml文件的主要内容,如果未能解决你的问题,请参考以下文章

delphi 如何把字符串转化为xml,最好只提供读取就好了,不用保存

c/c++转化为XML存储????

c# 如何将string转化为xml形式 再读取想要的节点

如何将html格式的string字符串转化为xml对象

如何将xml的数据转化为实体

xml格式转换为excel