jsp接收并输出dom4j解析xml
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@pageimport="read.ReadXml"%>
<%@pageimport="read.MenuBeanan"%>
<%@tagliburi="http://java.sun.com/jsp/jstl/core"prefix="c"%>
<%
ReadXmlreadx=newReadXml();
List<MenuBean>list=newArrayList<MenuBean>();
list=readx.readXml();
request.setAttribute("va",list);
%>
<head>
</head>
<html>
<body>
<c:forEachitems="${requestScope.va}"var="msg">
<ahref="${msg.link}">${msg.title}<br/></a>
</c:forEach>
</body>
</html>MenuBean.java
package read;
public class MenuBean {
privateStringtitle;
privateStringlink;
privateStringdescription;
publicMenuBean(Stringtitle,Stringlink,Stringdescription){
super();
this.title=title;
this.link=link;
this.description=description;
}
publicStringgetTitle(){
returntitle;
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicStringgetLink(){
returnlink;
}
publicvoidsetLink(Stringlink){
this.link=link;
}
publicStringgetDescription(){
returndescription;
}
publicvoidsetDescription(Stringdescription){
this.description=description;
}
}ReadXml.java
package read;
import java.util.ArrayList;
importjava.util.Iterator;
import java.util.List;import org.dom4j.Document;
importorg.dom4j.Element;
import org.dom4j.io.SAXReader;import read.MenuBean;
public class ReadXml {
@SuppressWarnings("unchecked")
publicList<MenuBean>readXml()throwsException{
SAXReaderreader=newSAXReader();
Documentdoc=reader.read("C://news.rdf.xml");
Elementroot=doc.getRootElement();
ElementnextElement=root.element("channel");
List<MenuBean>list=newArrayList<MenuBean>();
for(Iteratori=nextElement.elementIterator("item");i.hasNext();){
Elementfoo=(Element)i.next();
MenuBeanen=newMenuBean(foo.elementText("description"),foo.elementText("title"),foo.elementText("link"));
list.add(en);
}
returnlist;
}
}