五、显示xml内容:
package my_gan;
import home.study.bean.ViewInfo;
import java.io.FileInputStream;
import java.util.List;
import java.util.Vector;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class Main_Info {
private String path, urlto, type, title, link, id, pic,more,moretext, content;
public String getContent() {
return content;
}
public String getMore() {
return more;
}
public void setMore(String more) {
this.more = more;
}
public void setContent(String content) {
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
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 getUrlto() {
return urlto;
}
public void setUrlto(String urlto) {
this.urlto = urlto;
}
public Vector LoadXML(String path, String type0) throws Exception {
Vector xmlVector = null;
FileInputStream fi = null;
try {
fi = new FileInputStream(path);
xmlVector = new Vector();
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(fi);
Element root = doc.getRootElement(); // 得到根元素
List books = root.getChildren(); // 得到根元素所有子元素的集合
Element book = (Element) books.get(0);
Element typexml = (Element) book.getChild(type0);
List titles = typexml.getChildren("titles");
Main_Info xml = null;
//System.out.println(titles.size());
for (int i = 0; i < titles.size(); i++) {
xml = new Main_Info();
book = (Element) titles.get(i);
xml.setId(Integer.toString(i));
xml.setLink(book.getChild("link").getText());
xml.setPic(book.getChild("pictrue").getText());
xml.setContent(book.getChild("content").getText());
xml.setTitle(book.getChild("title").getText());
xml.setMore(book.getChild("more").getText());
xml.setMoretext(book.getChild("moretext").getText());
//System.out.println(book.getChild("link").getText());
xmlVector.add(xml);
}
} catch (Exception e) {
System.err.println(e + "error");
} finally {
try {
fi.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return xmlVector;
}
public String getMoretext() {
return moretext;
}
public void setMoretext(String moretext) {
this.moretext = moretext;
}
}
显示内容的jsp文件:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page language="java" import="java.util.*,jdom.test.*,home.study.bean.ViewInfo,my_gan.*"%>
<%
String path = application.getRealPath("/my/"+id+"/main.xml");
Main_Info xml = new Main_Info();
Vector xmlall = xml.LoadXML(path,type);
for (int i = 0; i < xmlall.size(); i++) {
xml = (Main_Info)xmlall.elementAt(i);
%>
<%}
<%=xml.getTitle()%>
<%=xml.getContent()%>
>
<%=xml.getMoretext()%>
修改|
删除
%>