AJAX 载入xml
来源:互联网 发布:linux开放22端口 编辑:程序博客网 时间:2024/04/29 23:53
简述:
载入XML文件(来自w3school)
知识点
1. 对xml文件内部元素的获取
2. xmlhttp.open("GET",'books.xml',true) 打开books.xml
3. x = xmlDoc.getElementsByTagName("title") 获取xml中所有标签为title的元素,存在一个list
4. xmlhttp.responseXML获取一个xml文件
books.xml
<?xml version="1.0" encoding="ISO-8859-1"?><bookstore> <book category="children"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="cooking"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="web" cover="paperback"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> <book category="web"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book></bookstore>
代码:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <script type="text/javascript"> function loadBooksXML(url) { /**************************** Intialize XMLHttpRequest *************************/ var xmlhttp = null; if(window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else if(window.ActiveXObject) {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //get the books.xml if(xmlhttp != null) { xmlhttp.open("GET",'books.xml',true); xmlhttp.send(); /******************************* OutPut Html *********************************/ xmlhttp.onreadystatechange = function() { //readyState = 4:hold complete request; status = 200:Http success if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { xmlDoc = xmlhttp.responseXML; txt = ""; // get a list the atrribute is "title" x = xmlDoc.getElementsByTagName('author'); for(var i = 0;i < x.length;i++) { //get data from XML tree txt = txt + x[i].childNodes[0].nodeValue + "<br />"; } document.getElementById("xml_field").innerHTML = txt; } }; /*****************************************************************************/ }else{ alert("Your browser does not support XMLHTTP"); } } </script></head><body> <h2> Read XML </h2> <button type = "button" onclick = "loadBooksXML('books.xml')">Get XML Info</button> <div id = "xml_field"></div></body></html>
输出:
点击前:
点击后:
- AJAX 载入xml
- 载入XML
- 载入XML
- AJAX异步载入文本
- xml文件载入
- AJAX分页以及IFRAME载入
- Ajax tooltip 动态ajax载入tips
- Ext+XML实现数据载入
- AJAX 载入JS文件,载入CSS文件,载入JS函数,执行JS 。ASP应用
- AJAX 载入JS文件,载入CSS文件,载入JS函数,执行JS 。ASP应用
- AJAX实现的载入等待效果
- ajax载入页面js失效的问题
- ajax原来也可以优化!!!!!惰性载入~~~~
- 一个重新载入struts_config.xml的问题
- xml+php动态载入与分页
- xml+php动态载入与分页
- 自定义载入类(如图片、XML)
- Unity3D结合XML载入和保存数据
- 自己典藏的软件
- linux 下安装 rar
- 常犯的10个MySQL错误
- JAVA设计模式:建造者(Builder)
- java面向对象6大原则2
- AJAX 载入xml
- 网卡的组成工作原理
- Android 图形用户界面 之 绘图(一)
- QDir::NoDotAndDotDot隐藏了所有的项目
- __main() 和 main()
- 创业者 VC为什么不给你见面机会
- 自签名,打包
- 职业规划
- MFC中timer 的使用