<%@ page language="java" import="java.util.*,java.text.*,java.io.*,java.util.Enumeration"
contentType="text/html; charset=utf-8"%>
<%@ page import="org.jdom.input.*"%>
<%@ page import="org.jdom.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>'JDomSAX解析XML</title>
</head>
<body>
<%
SAXBuilder builder = new SAXBuilder();
String pathStudents = request.getRealPath("students.xml");
Document doc = builder.build(pathStudents);
Element root = doc.getRootElement();
List children = root.getChildren();
String output = "<table border='1'>";
output += "<tr><td>姓名</td><td>学号</td><td>年龄</td><td>性别</td></tr>";
for (int i = 0; i < children.size(); i++) {
Element node = (Element) children.get(i);
Attribute attr = node.getAttribute("id");
output+="<tr><td>"+attr.getIntValue()+"</td>";
output+="<td>"+node.getChildText("name")+"</td>";
output+="<td>"+node.getChildText("age")+"</td>";
output+="<td>"+node.getChildText("gender")+"</td>";
output+="</tr>";
}
output+="</table>";
out.print(output);
%>
<br>
</body>
</html>
Jamespan
ziom
相关分类