这个操作还是比较简单的,就得加深记忆了,再次记录:
1,首先在这两步之前都需要先引入jquery的js进入jsp或者html
2,然后再说读取json文件,这个很简单,以ajax的方式get请求,datatype可写可不写,因为在请求的时候有关于消息头自动解析读取即可返回就是json数据,如果设置为text则返回的就是字符串,不是json
例子:
$(function(){
$.ajax({
url:'../js/chinaall.json',
type:'get',
dataType:'json',
success:function(data){
alert(JSON.stringify(data));
}
});
}
3,读取解析XML文件,还是以ajax的方式get请求datatype为xml或者不写,但是主要请求完成后回调函数里的值是不能够直接显示的,需要进行jquery解析用$(参数)生成可操作的jquery对象,然后再去用操作jquery元素的方式去遍历解析即可,网上资料查到的是用find,children,each等去遍历解析,但是本人测试发现,基本所有的jquery操作都可以执行。
例子:
var location=[];
var lslo="";
$.ajax({
url:'../js/map/svgmap/alldata.svg',
type:'get',
// dataType:'xml',
success:function(xml){
var dom = $(xml);
var test =dom.find("path").eq(0).attr("name");
alert(test);
dom.find("path").each(function(){
lslo=$(this).attr("d");
lslo=lslo.split("M")[1].split("c")[0];
var ls={
'name':$(this).attr("name"),
'locat':lslo
}
location.push(ls);
});
alert(JSON.stringify(location));
console.log(JSON.stringify(location));
}
});