java,jsp读取xml文件,并显示读取内容

本人是在jsp中写java文件,读取xml文件中的内容,并在页面显示。tangyj

1.下载jar包

2.写xml文件

3.在jsp中写java读取xml内容



<%@pageimport="org.jdom2.Document"%>

<%@ page import = "org.jdom2.input.SAXBuilder" %>
<%@ page import = "org.jdom2.output.Format" %>
<%@ page import = "org.jdom2.output.XMLOutputter" %>
<%@ page import = "org.jdom2.Element" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.util.*" %>
spacer.gif
加入jar包
jdom.jar

<%
String path=pageContext.getServletContext().getRealPath( "version.xml" );
System.out.println(path+ "----------------7----==---" );
SAXBuilder sb = new SAXBuilder(); // 新建立构造器
Document doc1 = sb.build( new FileInputStream(path));
Element root1=doc1.getRootElement();
String smtpusername= "" ; //用户名
String smtppwd= "" ; //密码
String smtpserver= "" ; //邮件服务器域名
Listlist1=root1.getChildren( "person" );
for ( int ii=0;ii<list1.size();ii++){
Element element=(Element)list1.get(ii);
smtpusername=element.getChildText( "name" );
smtppwd=element.getChildText( "ver" );
smtpserver=element.getChildText( "publish" );
System.out.println(smtpusername+ "--" +smtppwd+ "--" +smtpserver);
}
%>
将你读取的内容,在jsp页面用<%=smtpserver>显示即可

//这个是建立的xml文件
version.xml在我的根目录下,就是项目启动部署的目录
<? xml version = "1.0" encoding = "UTF-8" ?>
< version >
< person id = "01" >
< name >lhl </ name >
< ver > v1.0 </ ver >
< publish > 2013-08-22 </ publish >
< company > zkr </ company >
</ person >

< person id = "01" >
< name >tangyj </ name >
< ver > v1.1 </ ver >
< publish > 2013-08-25 </ publish >
< company >zkr </ company >
</ person >
</ version >


你可能感兴趣的:(java,jsp,读取xml文件并显示,采用jdom)