xml javascript
Posted konglingxi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml javascript相关的知识,希望对你有一定的参考价值。
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>
以上是关于xml javascript的主要内容,如果未能解决你的问题,请参考以下文章
AJAX 响应:数据(JSON、XML)还是 HTML 片段? [关闭]
VSCode自定义代码片段12——JavaScript的Promise对象
VSCode自定义代码片段12——JavaScript的Promise对象