XML、集合、JSP综合练习
2015-05-12 12:52
302 查看
一、利用DOM解析XML文件得到信息;存入泛型集合中在JSP页面循环打印读取的信息
a) 编写XML文件;添加测试节点数据
b) 建立web项目;在JSP页面中使用DOM解析XML技术得到XML中的新闻信息
c) 创建实体类news;对新闻实体类进行封装;使用新闻实体类对新闻数据进行封装
d) 创建新闻类泛型集合存储每条新闻信息
e) 在页面利用循环和显示标签展示数据
效果:
XML文件:
展示效果:
编写xml文件
创建实体类news;对新闻实体类进行封装;使用新闻实体类对新闻数据进行封装
用泛型集合从页面输出效果
a) 编写XML文件;添加测试节点数据
b) 建立web项目;在JSP页面中使用DOM解析XML技术得到XML中的新闻信息
c) 创建实体类news;对新闻实体类进行封装;使用新闻实体类对新闻数据进行封装
d) 创建新闻类泛型集合存储每条新闻信息
e) 在页面利用循环和显示标签展示数据
效果:
XML文件:
展示效果:
编写xml文件
<?xml version="1.0" encoding="UTF-8"?> -<news> -<new id="1"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="2"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="3"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="4"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="5"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="6"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="7"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="8"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="9"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="10"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> -<new id="11"> <title>关于依法治国论述摘编发行会见缅甸客人</title> <type>时政要闻</type> <date>2015-04-27 17:40</date> <author>小编</author> </new> </news>
创建实体类news;对新闻实体类进行封装;使用新闻实体类对新闻数据进行封装
package entity; /** * 新闻类 * @author Administrator * */ public class News { private String id;//新闻ID private String title;//标题 private String type;//新闻类型 private String date;//发布时间 private String author;//作者 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } }
用泛型集合从页面输出效果
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@page import="javax.xml.parsers.DocumentBuilderFactory"%> <%@page import="javax.xml.parsers.DocumentBuilder"%> <%@page import="org.w3c.dom.Document"%> <%@page import="org.w3c.dom.NodeList"%> <%@page import="org.w3c.dom.Node"%> <%@page import="entity.News"%> <%@page import="org.w3c.dom.Element"%> <% 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> <base href="<%=basePath%>"> <title>解析显示新闻</title> </head> <body> <% //建立dom解析器工厂实例 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); //得到解析器对象 DocumentBuilder db = dbf.newDocumentBuilder(); //得到web工程根目录 System.out.print(request.getRealPath("/")); //加载XML文件;创建docuemnt对象(生成dom树) Document dom = db.parse(request.getRealPath("/")+"/new.xml"); //开始解析新闻 NodeList nodelist = dom.getElementsByTagName("new"); //创建泛型集合存储解析的新闻信息 List<News> newslist = new ArrayList<News>(); //循环解析 for(int i = 0; i < nodelist.getLength(); i++){ //得到每个node(节点)对象 Node newnode = nodelist.item(i); //将节点对象转换为元素对象 Element element = (Element)newnode; //得到新闻的id属性 String newid = element.getAttribute("id"); //创建新闻对象 News news = new News(); //将id存储在新闻对象中 news.setId(newid); //获取其他节点属性 for(Node news_child = newnode.getFirstChild(); news_child != null;news_child = news_child.getNextSibling()){ //判断当前是元素对象还是节点对象 if(news_child.getNodeType() == Node.ELEMENT_NODE){ if(news_child.getNodeName().equals("title")){ //获取新闻标题;将标题封装到对象中 news.setTitle(news_child.getFirstChild().getNodeValue()); } if(news_child.getNodeName().equals("type")){ //获取新闻类型;将标题封装到对象中 news.setType(news_child.getFirstChild().getNodeValue()); } if(news_child.getNodeName().equals("date")){ //获取新闻发布时间;将标题封装到对象中 news.setDate(news_child.getFirstChild().getNodeValue()); } if(news_child.getNodeName().equals("author")){ //获取新闻作者;将标题封装到对象中 news.setAuthor(news_child.getFirstChild().getNodeValue()); } } } //将新闻对象放入泛型集合中 newslist.add(news); } %> <center> <table style="width: 800px; height: auto; border:1px solid #900;font-size: 12px" cellpadding="0" cellspacing="0"> <tr style="text-align: center;background: #900; color: #ffffff;border: 1px solid #900"> <td>新闻ID</td> <td>新闻标题</td> <td>新闻类型</td> <td>发布时间</td> <td>发布作者</td> </tr> <% //循环新闻列表 for(int i = 0; i < newslist.size(); i ++){ //取出集合中每个新闻实体对象 News news = newslist.get(i); %> <tr> <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getId() %></td> <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getTitle() %></td> <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getType() %></td> <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getDate() %></td> <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getAuthor() %></td> </tr> <% } %> </table> </center> </body> </html>
相关文章推荐
- 46-IO流(Properties集合的-练习)12 47-IO流(综合练习-文件清单列表. 48-IO流(打印流-PrintStream) 49-IO流(打印流-PrintWriter).
- jsp的综合练习,一个简单的登陆系统
- 集合与IO流综合练习
- day23_File对象-练习++Properties集合的+综合练习+打印流+序列流
- Xml 丶json丶 C/S KVO 数据库 SQL 数据持久化 复杂对象 集合视图综合
- 黑马程序员——JAVA基础--- 集合IO综合练习排序问题
- SpringMVC+Spring+MyBatis 的综合练习 4 (配置 web.xml )
- JSP简单练习-包装类综合应用实例
- 初识.net界面程序(14)--WPF综合练习(3)LINQ TO XML
- JSP数据库综合练习
- android基础篇------------java基础(9)(集合的综合练习)
- JavaSE 练习对面向对象思想、单列集合、双列集合的综合运用 - 斗地主
- 集合的综合练习:Poker牌
- JSP简单练习-包装类综合应用实例
- 牛客网练习知识点笔记——综合
- C程序设计综合练习--基础编程1:
- JSP基础练习---登录验证
- dom的综合练习
- jsp2.0中web.xml 的元素
- 普及练习场 BOSS战-普及综合练习1 幻想迷宫