xml javascript
2024-10-16 08:05:55
1.XMLHttpRequest对象
创建XMLHttpRequest对象
xmlhttp=new XMLHttpRequest(); ##老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 对象: xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
例子:
<html> <head> <script type="text/javascript"> var xmlhttp; function loadXMLDoc(url) { xmlhttp=null; if (window.XMLHttpRequest) {// code for IE7, Firefox, Opera, etc. xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp!=null) { xmlhttp.onreadystatechange=state_Change; xmlhttp.open("GET",url,true); xmlhttp.send(null); } else { alert("Your browser does not support XMLHTTP."); } } function state_Change() { if (xmlhttp.readyState==4) {// 4 = "loaded" if (xmlhttp.status==200) {// 200 = "OK" document.getElementById('A1').innerHTML=xmlhttp.status; document.getElementById('A2').innerHTML=xmlhttp.statusText; document.getElementById('A3').innerHTML=xmlhttp.responseText; } else { alert("Problem retrieving XML data:" + xmlhttp.statusText); } } } </script> </head> <body> <h2>Using the HttpRequest Object</h2> <p><b>Status:</b> <span id="A1"></span> </p> <p><b>Status text:</b> <span id="A2"></span> </p> <p><b>Response:</b> <br /><span id="A3"></span> </p> <button onclick="loadXMLDoc('http://www.w3school.com.cn/example/xdom/note.xml')">Get XML</button> </body> </html>
2.解析xml字符串
Internet Explorer 使用 loadXML() 方法来解析 XML 字符串,而其他浏览器使用 DOMParser 对象。
txt="<bookstore><book>"; txt=txt+"<title>Everyday Italian</title>"; txt=txt+"<author>Giada De Laurentiis</author>"; txt=txt+"<year>2005</year>"; txt=txt+"</book></bookstore>"; if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(txt); }
3.xml DOM
a.解析xml文件
<html> <body> <h1>W3School.com.cn Internal Note</h1> <p> <b>To:</b> <span id="to"></span><br /> <b>From:</b> <span id="from"></span><br /> <b>Message:</b> <span id="message"></span> </p> <script type="text/javascript"> 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","/example/xmle/note.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; </script> </body> </html>
b.解析xml字符串
<html> <body> <h1>W3School.com.cn Internal Note</h1> <p> <b>To:</b> <span id="to"></span><br /> <b>From:</b> <span id="from"></span><br /> <b>Message:</b> <span id="message"></span> </p> <script> txt="<note>"; txt=txt+"<to>George</to>"; txt=txt+"<from>John</from>"; txt=txt+"<heading>Reminder</heading>"; txt=txt+"<body>Don't forget the meeting!</body>"; txt=txt+"</note>"; if (window.DOMParser) { parser=new DOMParser(); xmlDoc=parser.parseFromString(txt,"text/xml"); } else // Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.async="false"; xmlDoc.loadXML(txt); } document.getElementById("to").innerHTML=xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue; document.getElementById("from").innerHTML=xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue; document.getElementById("message").innerHTML=xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue; </script> </body> </html>
最新文章
- django和apache交互的wsgi分析
- apache2服务器mod_rewrite模块 开启方法[linux, ubuntu]
- uva 725 division(水题)——yhx
- 百度和 Google 的搜索技术是一个量级吗?
- 移动周报:十款最实用的Android UI设计工具
- C++----练习--bool类型作为特别的int要区别对待
- 处理date类型对象的方式
- Eclipse UML小工具AmaterasUML的配置和使用
- 本地存储 cookie,session,localstorage( 二)angular-local-storage
- sort 排序详解
- Git相关操作一
- React---简单实现表单点击提交插入、删除操作
- div介绍 盒子模型边框属性 CSS初始化 文字排版 边框调整 溢出
- SpringBoot+Mybatis实现关联查询
- git忽视修改的文件
- Sprint冲刺第二阶段之6---10天(下)
- Linux 下 wordpress 无法安装插件
- SpringBoot进阶之web进阶
- NumPy 位运算
- 解题:JSOI 2011 柠檬