XML HTTP传输 小结

what's XML

XML 指可扩展标记语言(eXtensible Markup Language)。

XML 被设计用来传输和存储数据,不用于表现和展示数据,HTML 则用来表现数据。

XML 是独立于软件和硬件的信息传输工具。

应该掌握的基础知识

  • HTML
  • JavaScript

XML 不会做任何事情

XML 被设计用来结构化、存储以及传输信息。

  • 文件格式
xml 复制代码
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>

没有预定义标签

  • HTML 中使用的标签都是预定义的。HTML 文档只能使用在 HTML 标准中定义过的标签(如 <p>、<h1> 等等)。
  • 上面实例中的标签没有在任何 XML 标准中定义过(比如 和 ),
  • XML 允许创作者定义自己的标签和自己的文档结构。

简化数据共享

  • XML 数据以纯文本格式进行存储,因此提供了一种独立于软件和硬件的数据存储方法。

real example

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>	
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
  • line 1定义XML 的版本(1.0)和所使用的编码(UTF-8 : 万国码, 可显示各种语言)

树结构

xml 复制代码
<bookstore>
    <book category="COOKING">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="CHILDREN">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="WEB">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
</bookstore>

使用CSS格式化显示XML

xml 复制代码
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
.
.
.
</CATALOG>
  • 第二行把 XML 文件链接到 CSS 文件

XSLT

JS API of XML

  • create an XMLHttpRequest Object
xml 复制代码
xmlhttp=new XMLHttpRequest();
  • XML文档->XML DOM
xml 复制代码
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","books.xml",false);	//同步不需要编写onreadystate处理服务器响应
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
  • 异步响应
html 复制代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
<script>
function loadXMLDoc()
{
  if (window.XMLHttpRequest)
  {
    // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp=new XMLHttpRequest();
  }
  else
  {
    // IE6, IE5 浏览器执行代码
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }

  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("GET","xmlhttp_info.txt",true);
  xmlhttp.send();
}
</script>
</head>
<body>

<h2>使用 XMLHttpRequest 对象</h2>
<div id="myDiv"></div>
<button type="button" onclick="loadXMLDoc()">修改内容</button>

</body>
</html>

XMLHttpRequest GET

  • 所有现代的浏览器(IE7+、Firefox、Chrome、Safari 和 Opera)都有一个内建的 XMLHttpRequest 对象。
  • 旧版本的 Internet Explorer(IE5 和 IE6)使用 ActiveX 对象:xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  1. 发送一个请求到服务器
xml 复制代码
xmlhttp.open("GET","xmlhttp_info.txt",true);
xmlhttp.send();

format:open(method,url,async)

  • method:请求的类型:GET 或 POST
  • url:文件在服务器上的位置
  • async:true(异步)或 false(同步)
  1. 处理服务器的相应数据
  • 返回的是字符串
xml 复制代码
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  • 返回的是XML文档对象
xml 复制代码
xmlDoc=xmlhttp.responseXML;
var txt="";
x=xmlDoc.getElementsByTagName("ARTIST");
for (i=0;i<x.length;i++)
{
  txt=txt + x[i].childNodes[0].nodeValue + "";
}
document.getElementById("myDiv").innerHTML=txt;
  1. onreadystatechange
xml 复制代码
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
}
  • onreadystatechange:存储函数(或函数的名称)在每次 readyState 属性变化时被自动调用
  • readyState存放了 XMLHttpRequest 的状态。从 0 到 4 变化:
    0:请求未初始化
    1:服务器建立连接
    2:收到的请求
    3:处理请求
    4:请求完成和响应准备就绪
  • status:200:"OK"
    404:找不到页面

XMLHttpRequest POST

Mylab

  1. transform image to xml
  2. http post image,and get response XML tag
  3. transform xml into text and show on the browser
相关推荐
Predestination王瀞潞31 分钟前
2.4 编码->W3C XML 1.0标准(W3C Recommendation):XML(Extensible Markup Language)
xml·前端
2501_9160074738 分钟前
HTTPS 抓包的流程,代理抓包、设备数据线直连抓包、TCP 数据分析
网络协议·tcp/ip·ios·小程序·https·uni-app·iphone
IpdataCloud2 小时前
资源受限设备上轻量级IP查询模块的部署方法
网络·数据库·网络协议·tcp/ip
eleven40962 小时前
穿透内容审查与阻断:基于 DNS TXT 记录的动态服务发现与客户端安全加固实践
网络协议·ios·app
魑魅魍魉都是鬼5 小时前
TCP、UDP Http Https
tcp/ip·http·udp
tzy2336 小时前
HTTPS 认证过程
网络协议·http·https
跨境海王哥6 小时前
怎么检查一个IP是否干净?IP质量分数检测及如何判断风险?
网络·网络协议·tcp/ip
nainaire6 小时前
仿muduo库的Tcp服务器以及其应用层Http协议支持
服务器·网络·c++·tcp/ip·http
老兵发新帖6 小时前
查看fail2ban停止的IP和历史记录
chrome·网络协议·tcp/ip
森叶6 小时前
深入理解 Hash:它不是一个函数,而是一种思想
人工智能·http·架构