XML、集合、JSP综合练习

一、利用DOM解析XML文件得到信息;存入泛型集合中在JSP页面循环打印读取的信息

a)         编写XML文件;添加测试节点数据

b)         建立web项目;在JSP页面中使用DOM解析XML技术得到XML中的新闻信息

c)         创建实体类news;对新闻实体类进行封装;使用新闻实体类对新闻数据进行封装

d)         创建新闻类泛型集合存储每条新闻信息

e)         在页面利用循环和显示标签展示数据

效果:

XML文件:

展示效果:

编写xml文件

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 
  3 -<news>
  4 
  5 
  6 -<new id="1">
  7 
  8 <title>关于依法治国论述摘编发行会见缅甸客人</title>
  9 
 10 <type>时政要闻</type>
 11 
 12 <date>2015-04-27 17:40</date>
 13 
 14 <author>小编</author>
 15 
 16 </new>
 17 
 18 
 19 -<new id="2">
 20 
 21 <title>关于依法治国论述摘编发行会见缅甸客人</title>
 22 
 23 <type>时政要闻</type>
 24 
 25 <date>2015-04-27 17:40</date>
 26 
 27 <author>小编</author>
 28 
 29 </new>
 30 
 31 
 32 -<new id="3">
 33 
 34 <title>关于依法治国论述摘编发行会见缅甸客人</title>
 35 
 36 <type>时政要闻</type>
 37 
 38 <date>2015-04-27 17:40</date>
 39 
 40 <author>小编</author>
 41 
 42 </new>
 43 
 44 
 45 -<new id="4">
 46 
 47 <title>关于依法治国论述摘编发行会见缅甸客人</title>
 48 
 49 <type>时政要闻</type>
 50 
 51 <date>2015-04-27 17:40</date>
 52 
 53 <author>小编</author>
 54 
 55 </new>
 56 
 57 
 58 -<new id="5">
 59 
 60 <title>关于依法治国论述摘编发行会见缅甸客人</title>
 61 
 62 <type>时政要闻</type>
 63 
 64 <date>2015-04-27 17:40</date>
 65 
 66 <author>小编</author>
 67 
 68 </new>
 69 
 70 
 71 -<new id="6">
 72 
 73 <title>关于依法治国论述摘编发行会见缅甸客人</title>
 74 
 75 <type>时政要闻</type>
 76 
 77 <date>2015-04-27 17:40</date>
 78 
 79 <author>小编</author>
 80 
 81 </new>
 82 
 83 
 84 -<new id="7">
 85 
 86 <title>关于依法治国论述摘编发行会见缅甸客人</title>
 87 
 88 <type>时政要闻</type>
 89 
 90 <date>2015-04-27 17:40</date>
 91 
 92 <author>小编</author>
 93 
 94 </new>
 95 
 96 
 97 -<new id="8">
 98 
 99 <title>关于依法治国论述摘编发行会见缅甸客人</title>
100 
101 <type>时政要闻</type>
102 
103 <date>2015-04-27 17:40</date>
104 
105 <author>小编</author>
106 
107 </new>
108 
109 
110 -<new id="9">
111 
112 <title>关于依法治国论述摘编发行会见缅甸客人</title>
113 
114 <type>时政要闻</type>
115 
116 <date>2015-04-27 17:40</date>
117 
118 <author>小编</author>
119 
120 </new>
121 
122 
123 -<new id="10">
124 
125 <title>关于依法治国论述摘编发行会见缅甸客人</title>
126 
127 <type>时政要闻</type>
128 
129 <date>2015-04-27 17:40</date>
130 
131 <author>小编</author>
132 
133 </new>
134 
135 
136 -<new id="11">
137 
138 <title>关于依法治国论述摘编发行会见缅甸客人</title>
139 
140 <type>时政要闻</type>
141 
142 <date>2015-04-27 17:40</date>
143 
144 <author>小编</author>
145 
146 </new>
147 
148 </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;
    }
    
    

}

用泛型集合从页面输出效果

  1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
  2 <%@page import="javax.xml.parsers.DocumentBuilderFactory"%>
  3 <%@page import="javax.xml.parsers.DocumentBuilder"%>
  4 <%@page import="org.w3c.dom.Document"%>
  5 <%@page import="org.w3c.dom.NodeList"%>
  6 <%@page import="org.w3c.dom.Node"%>
  7 <%@page import="entity.News"%>
  8 <%@page import="org.w3c.dom.Element"%>
  9 <%
 10 String path = request.getContextPath();
 11 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 12 %>
 13 
 14 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 15 <html>
 16   <head>
 17     <base href="<%=basePath%>">
 18     
 19     <title>解析显示新闻</title>
 20     
 21   </head>
 22   
 23   <body>
 24     <%
 25         //建立dom解析器工厂实例
 26         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 27         
 28         //得到解析器对象 
 29         DocumentBuilder db = dbf.newDocumentBuilder();
 30 
 31     //得到web工程根目录
 32         System.out.print(request.getRealPath("/"));
 33         //加载XML文件;创建docuemnt对象(生成dom树)
 34         Document dom = db.parse(request.getRealPath("/")+"/new.xml");
 35         
 36         //开始解析新闻
 37         NodeList nodelist = dom.getElementsByTagName("new");
 38         
 39         //创建泛型集合存储解析的新闻信息
 40         List<News> newslist = new ArrayList<News>();
 41         
 42         //循环解析
 43         for(int i = 0; i < nodelist.getLength(); i++){
 44             
 45             //得到每个node(节点)对象
 46             Node newnode = nodelist.item(i);
 47             
 48             //将节点对象转换为元素对象
 49             Element element = (Element)newnode;
 50             
 51             //得到新闻的id属性
 52             String newid = element.getAttribute("id");
 53             
 54             //创建新闻对象
 55             News news = new News();
 56             
 57             //将id存储在新闻对象中
 58             news.setId(newid);
 59             
 60             //获取其他节点属性
 61             for(Node news_child = newnode.getFirstChild(); news_child != null;news_child = news_child.getNextSibling()){
 62                 
 63                 //判断当前是元素对象还是节点对象 
 64                 if(news_child.getNodeType() == Node.ELEMENT_NODE){
 65                     
 66                     if(news_child.getNodeName().equals("title")){
 67                         //获取新闻标题;将标题封装到对象中
 68                         news.setTitle(news_child.getFirstChild().getNodeValue());
 69                     }
 70                     if(news_child.getNodeName().equals("type")){
 71                         //获取新闻类型;将标题封装到对象中
 72                         news.setType(news_child.getFirstChild().getNodeValue());
 73                     }
 74                     if(news_child.getNodeName().equals("date")){
 75                         //获取新闻发布时间;将标题封装到对象中
 76                         news.setDate(news_child.getFirstChild().getNodeValue());
 77                     }
 78                     if(news_child.getNodeName().equals("author")){
 79                         //获取新闻作者;将标题封装到对象中
 80                         news.setAuthor(news_child.getFirstChild().getNodeValue());
 81                     }
 82                     
 83                 }
 84             }
 85             //将新闻对象放入泛型集合中
 86             newslist.add(news);
 87         }
 88     %>
 89     
 90     <center>
 91         <table style="width: 800px; height: auto; border:1px solid #900;font-size: 12px" cellpadding="0" cellspacing="0">
 92             <tr style="text-align: center;background: #900; color: #ffffff;border: 1px solid #900">
 93                 <td>新闻ID</td>
 94                 <td>新闻标题</td>
 95                 <td>新闻类型</td>
 96                 <td>发布时间</td>
 97                 <td>发布作者</td>
 98             </tr>
 99             <%
100                 //循环新闻列表
101                 for(int i = 0; i < newslist.size(); i ++){
102                     //取出集合中每个新闻实体对象
103                     News news = newslist.get(i);
104             %>
105             <tr>
106                 <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getId() %></td>
107                 <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getTitle() %></td>
108                 <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getType() %></td>
109                 <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getDate() %></td>
110                 <td style="text-align: center;border: 1px solid #900;height:30px"><%=news.getAuthor() %></td>
111             </tr>
112             <%
113                 }
114             %>
115         </table>
116     </center>
117   </body>
118 </html>

 

posted on 2015-05-12 12:52  梦之航  阅读(369)  评论(0编辑  收藏  举报

导航