zoukankan      html  css  js  c++  java
  • JSP简单的练习-使用JDOM阅读xml文件

    <%@ page contentType="text/html; charset=gb2312" language="java" %>
    <%@ page import="java.io.*,org.jdom.*,org.jdom.input.*,org.jdom.output.*,java.util.List,java.util.Iterator" %>
    <html>
    <head>
    <title>用JDOM解析并输出user.xml</title>
    </head>
    <body>
    	<table>
    		<!-- 输出表头 -->
    		<tr>
    			<td>用户ID</td>
    			<td>username</td>
    			<td>password</td>
    			<td>真实姓名</td>
    			<td>年龄</td>
    			<td>性别</td>
    		</tr>
    		<%  // 得到数据
    			SAXBuilder builder=new SAXBuilder(); // 创建对象
    			// 建立Document对象
    			 Document readDocument=builder.build(pageContext.getServletContext().getResourceAsStream("/user.xml")); 
    			 // 注意:user.xml文件要和该文件放到一个目录下。详细原因如今不太清楚,是实验得到的
    			// 得到根元素
    			 Element rootElement=readDocument.getRootElement();
    			// 得到根元素的子元素列表,实际上就是user元素列表
    			List list=rootElement.getChildren();
    			// 输出数据
    			for(Iterator i=list.iterator();i.hasNext();)
    			{
    				Element current=(Element)i.next();
    				out.println("<tr>");
    				// 输出用户ID号
    				out.println("<td>"+current.getChildText("id")+"</td>");
    				// 输出username
    				out.println("<td>"+current.getChildText("name")+"</td>");
    				// 输出用户password
    				out.println("<td>"+current.getChildText("password")+"</td>");
    				// 输出真实姓名
    				out.println("<td>"+current.getChildText("true_name")+"</td>");
    				// 输出用户年龄
    				out.println("<td>"+current.getChildText("age")+"</td>");
    				// 输出用户性别
    				out.println("<td>"+current.getChildText("sex")+"</td>");
    				out.println("</tr>");
    			} 
    		%>
    	</table>
    </body>
    </html>

    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    阶梯博弈
    hihoCoder #1199 : Tower Defense Game ——(树型dp)
    2016 China-Final-F题 ——(SA+二分)
    ACM之路(20)—— Splay初探
    2016 ICPC China-Final 现场赛总结
    【Interleaving String】cpp
    【Best Time to Buy and Sell Stock III 】cpp
    【Maximal Rectangle】cpp
    【palindrome partitioning II】cpp
    【Maximum Subarray 】cpp
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4713798.html
Copyright © 2011-2022 走看看