xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if (xhr.status==200) {
var data=xhr.responseXML;
}
}
}
XML数据格式
元数据:描述数据的数据。(下表中的标签就是元数据)
XML数据格式的弊端:
a:元数据占用数据量较大,不利于大量数据的网络传输
b:解析不太方便
//data.XML
三国演义
罗贯中
一个杀伐纷争的年代
水浒传
施耐庵
108条好汉的故事
XML解析过程
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if (xhr.status==200) {
var data=xhr.responseXML;
var bookList=data.getElementByTagName("booklist");
var books=bookList[0].children;
var book1=books[0];
方法一
var name=book1.children[0].innerHTML;//获取到data.xml中三国演义
var author=book1.children[0].innerHTML;//获取到data.xml中罗贯中
var desc=book1.children[0].innerHTML;//获取到data.xml中一个杀伐纷争的年代
方法二
var name=book1.childen[0].firstChild.wholeText;//获取到data.xml中三国演义
var author=book1.childen[1].firstChild.wholeText;//获取到data.xml中罗贯中
var desc=book1.childen[2].firstChild.wholeText;//获取到data.xml中个杀伐纷争的年代
}
}
}
json数据格式
定义:
json数据和普通的js对象的区别:
json数据没有变量
json形式的数据结尾没有分号
json数据中的键必须用双引号包住
//json 数据格式
{
"name":"调皮的酒窝",
"age":"20",
"lover":["LoL","singing","swimming"],
"friend":{
"name":"等待的夏天",
"high":"170cm",
"wejght":"80kg",
}
}
JSON解析过程
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if (xhr.status==200) {
var data=xhr.responseText;
var d=JSON.parse(data);
console.log(d.name);//
console.log(d.age);
console.log(d.friend.name);
console.log(d.friend.lover);
var tag='
'+d.name+' '+d.age+' '+d.friend.name+' '+d.lover+' '}
}
}
JSON.parse:将字符串变为对象
JSON.string:将对象变为字符串
var str='{"name":"坦率的向日葵","age":"20"}';
var obj=JSON.parse(str);//把JSON形式字符串变为7码计划科学打法g",
}
}
JSON解析过程
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if (xhr.status==200) {
var data=xhr.responseText;
var d=JSON.parse(data);
console.log(d.name);//
console.log(d.age);
console.log(d.friend.name);
console.log(d.friend.lover);
var tag='
'+d.name+' '+d.age+' '+d.friend.name+' '+d.lover+' '}
}
}
JSON.parse:将字符串变为对象
JSON.string:将对象变为字符串
var str='{"name":"坦率的向日葵","age":"20"}';
var obj=JSON.parse(str);//把JSON形式字符串变为对象
var str1=JSON.stringify(obj);//把对象转换为JSON形式的字符串
console.log(obj);//Object
console.log(str1);//{"name":"坦率的向日葵","age":"20"}
json_encode()作用:就是把数组转化为json格式的字符串