<%@ page language="java" import="javax.xml.parsers.*,org.w3c.dom.*" pageEncoding="utf-8"%><!-- 载入XML需的包 -->
<html>
<head>
<title>XMLRead</title>
</head>
<body>
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document d = db.parse(request.getRealPath("room.xml"));//建立Document对象,并设置文件路径参数
Element e = d.getDocumentElement();//取得代表整个XML文件内容的元素对象
NodeList nl = e.getElementsByTagName_r("room");//从XML元素当中萃取出所有包含名称为customer的节点集合对象NodeList
//依次将节点集合当中的元素并逐一取出显示在页面上
for (int i = 0; i < nl.getLength(); i++) {
Element childElement = (Element) nl.item(i);//取得目前索引位置的元素
NodeList childNodeList = null;
String outString = "";
//取得此元素底下的所有节点集合
childNodeList = childElement.getElementsByTagName_r("weekday");
outString = childNodeList.item(0).getFirstChild().getNodeValue();
out.println("weekday:" + outString + "<br>");
childNodeList = childElement.getElementsByTagName_r("place");
outString = childNodeList.item(0).getFirstChild().getNodeValue();
out.println("place:" + outString + "<br>");
childNodeList = childElement.getElementsByTagName_r("time");
outString = childNodeList.item(0).getFirstChild().getNodeValue();
out.println("time:" + outString + "<br>");
childNodeList = childElement.getElementsByTagName_r("class");
outString = childNodeList.item(0).getFirstChild().getNodeValue();
out.println("class:" + outString + "<br>");
out.println("<p>");
}
%>
</body>
</html>
<?xml version="1.0" encoding="utf-8" ?>
<空闲自习室数据>
</空闲自习室数据>
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
</web-app>