读取xml内容,并显示在jsp页面上
<persons>
<person>
<name>小东</name>
<address>china</address>
</person>
...
</persons>
在jsp里什么读取?
var http_request = false;
function send_request(url) {//初始化、指定处理函数、发送请求的函数
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
http_request.onreadystatechange = processRequest;
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET", url, true);
http_request.send(null);
}
// 处理返回信息的函数
function processRequest() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
var returnObj = http_request.responseXML;
var xmlobj = http_request.responseXML;
var employees = xmlobj.getElementsByTagName("channel");
var feedbackStr = "";
for(var i=0;i<employees.length;i++) { // 循环读取employees.xml的内容
var employee = employees[i];
feedbackStr += "员工:" + employee.getElementsByTagName("category")[0].firstChild.data;
feedbackStr += " 职位:" + employee.getElementsByTagName("title")[0].firstChild.data;
feedbackStr += " 工资:" + employee.getElementsByTagName("link")[0].firstChild.data;
feedbackStr += "\r\n";
}
alert(feedbackStr);
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
var feedbackStr = "";
这个是文件xml地址吗
feedbackStr 这个只是将取出来的内容放到这里面,你调用这个函数的时候将URL放到send_request(url) 这个参数中
<?xml version = "1.0" encoding = "UTF-8"?>
<Persons>
<Person>
<Name>Smith</Name>
<Department>Tech</Department>
<Position>Staff</Position>
<Salary>2000.00</Salary>
</Person>
<Person>
<Name>Peter</Name>
<Department>Sales</Department>
<Position>Staff</Position>
<Salary>2500.00</Salary>
</Person>
<Person>
<Name>Victor</Name>
<Department>Service</Department>
<Position>Manager</Position>
<Salary>4000.00</Salary>
</Person>
<Person>
<Name>Leon</Name>
<Department>Tech</Department>
<Position>Manager</Position>
<Salary>4500.00</Salary>
</Person>
</Persons>
这个是xml文件
<%@ page language="java" import="java.util.*,java.sql.*" %>
<%@ page contentType="text/html;charset=gb2312"%>
<html>
<body>
<script language="java script">
var http_request = false;
function send_request(xml.xml) {//初始化、指定处理函数、发送请求的函数
http_request = false;
//开始初始化XMLHttpRequest对象
if(window.XMLHttpRequest) { //Mozilla 浏览器
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {//设置MiME类别
http_request.overrideMimeType('text/xml');
}
}
else if (window.ActiveXObject) { // IE浏览器
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
}
if (!http_request) { // 异常,创建对象实例失败
window.alert("不能创建XMLHttpRequest对象实例.");
return false;
}
http_request.onreadystatechange = processRequest;
// 确定发送请求的方式和URL以及是否同步执行下段代码
http_request.open("GET", "xml", true);
http_request.send(null);
}
// 处理返回信息的函数
function processRequest() {
if (http_request.readyState == 4) { // 判断对象状态
if (http_request.status == 200) { // 信息已经成功返回,开始处理信息
var returnObj = http_request.responseXML;
var xmlobj = http_request.responseXML;
var employees = xmlobj.getElementsByTagName("channel");
var feedbackStr = "";
for(var i=0;i<employees.length;i++) { // 循环读取employees.xml的内容
var employee = employees[i];
feedbackStr += "Name:" + employee.getElementsByTagName("category")[0].firstChild.data;
feedbackStr += "Position:" + employee.getElementsByTagName("title")[0].firstChild.data;
feedbackStr += "Salary:" + employee.getElementsByTagName("link")[0].firstChild.data;
feedbackStr += "\r\n";
}
alert(feedbackStr);
} else { //页面不正常
alert("您所请求的页面有异常。");
}
}
}
</script>
</body>
</html>
这是jsp,但是读不出东西来啊
var employees = xmlobj.getElementsByTagName("Person");
var feedbackStr = "";
for(var i=0;i<employees.length;i++) { // 循环读取employees.xml的内容
var employee = employees[i];
feedbackStr += "Name:" + employee.getElementsByTagName("Name")[0].firstChild.data;
feedbackStr += "Position:" + employee.getElementsByTagName("Department")[0].firstChild.data;
feedbackStr += "Salary:" + employee.getElementsByTagName("Position")[0].firstChild.data;
feedbackStr += "\r\n";
指定目录下如:d:/xml.xml然后URL就是这个