我正在读取XML文件中的数据,并希望使用angularjs进行编辑。问题是,$http
调用是读取数据但是将其转换为字符串,我是否期望它是对象。我的XML数据是:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<check1 version="1"></check1>
<collection name="argu.ments"/>
<check3></check3>
<check4/>
<acollection name="arguments"/>
<string name="connect_timeout"></string>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
在angularjs中使用以下代码:
$http({
url: "books.xml",
method: "GET",
headers: {'Content-Type': 'application/xml','charset' : 'utf-8'}
}).success(function (xml) {
console.log(xml);
console.log(typeof xml);
}).error(function () {
console.log('error in /writexmlfiletest ');
});
它在控制台中打印数据,typeof
显示字符串。请帮忙理解。我是否必须转换回XML以执行节点元素操作?
答案 0 :(得分:0)
我们必须将transformResponse放在$ http请求中,如下所示。它将数据解析回xml。
$http({
url: "books.xml",
method: "GET",
headers: {'Content-Type': 'application/xml','charset' : 'utf-8'},
transformResponse : function(data) {
// string -> XML document object
return $.parseXML(data);
}
}).success(function (xml) {
console.log(xml);
console.log(typeof xml);
}).error(function () {
console.log('error in /writexmlfiletest ');
});