Java + XML = JDOM

Java+XML=JDOM!
这就是JDOM设计者的目标。如果你曾经使用过烦人的SAX或是DOM来处理XML,你就会知道为 什么要有JDOM或者是JAXB。在今年(2002)的JavaOne会议上JDOM的主要创始人JasonHunter有一篇精彩的演讲介绍了 JDOM技术,题目就是JDOMMakesXMLEasy。
获得并安装JDOM
http://jdom.org可 以下载JDOM的最新版本。以JDOMbeta8的2进制版本为例。下载后解压缩,JDOM的jar文件就是build目录下的文件jdom.jar, 将之加入类路径。另外JDOM还需要lib目录下那些jar文件如xerces.jar,jaxp.jar的支持。如果在使用中出现以下错误:
java.lang.NoSuchMethodError

java.lang.NoClassDefFoundError:org/xml/sax/SAXNotRecognizedException
你需要保证xerces.jar文件在CLASSPATH中位于其他XML类,如JAXP或Crimson之前,这些类文件,包括以前老版本的xerces,可能不支持SAX2.0或DOMLevel2。于是导致了上面的错误。

一个简单的例子
JDOM的处理方式有些类似于DOM,但它主要是用SAX实现的,你不必担心处理速度和内存的问题。另外,JDOM中几乎没有接口,的类全部是实实在在的类,没有类工厂类的。

下面是实例用的XML文件:

<?xmlversion="1.0"encoding="GBK"?>
<书库>
<书>
<书名>Java编程入门</书名>
<作者>张三</作者>
<出版社>电子出版社</出版社>
<价格>35.0</价格>
<出版日期>2002-10-07</出版日期>
</书>
<书>
<书名>XML在Java中的应用</书名>
<作者>李四</作者>
<出版社>希望出版社</出版社>
<价格>92.0</价格>
<出版日期>2002-10-07</出版日期>
</书>
</书库>

下面是操作XML文件的Bean:
packagexml;
/**
*XML的读写操作Bean
*/
importjava.io.*;
importjava.util.*;
importorg.jdom.*;
importorg.jdom.output.*;
importorg.jdom.input.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
publicclassXmlBean{
privateStringbookname,author,pub,price,pubdate;
publicStringgetbookname(){returnbookname;}
publicStringgetauthor(){returnauthor;}
publicStringgetpub(){returnpub;}
publicStringgetprice(){returnprice;}
publicStringgetpubdate(){returnpubdate;}
publicvoidsetbookname(Stringbookname){this.bookname=bookname;}
publicvoidsetauthor(Stringauthor){this.author=author;}
publicvoidsetpub(Stringpub){this.pub=pub;}
publicvoidsetprice(Stringprice){this.price=price;}
publicvoidsetpubdate(Stringpubdate){this.pubdate=pubdate;}
publicXmlBean(){}
/**
*读取XML文件所有信息
*/
publicVectorLoadXML(Stringpath)throwsException{
VectorxmlVector=null;
FileInputStreamfi=null;
try{
fi=newFileInputStream(path);
xmlVector=newVector();
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//得到根元素
Listbooks=root.getChildren();//得到根元素所有子元素的集合
Elementbook=null;
XmlBeanxml=null;
for(inti=0;i<books.size();i++){
xml=newXmlBean();
book=(Element)books.get(i);//得到第一本书元素
xml.setbookname(book.getChild("书名").getText());
xml.setauthor(book.getChild("作者").getText());
xml.setpub(book.getChild("出版社").getText());
xml.setprice(book.getChild("价格").getText());
xml.setpubdate(book.getChild("出版日期").getText());
xmlVector.add(xml);
}
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
returnxmlVector;
}
/**
*删除XML文件指定信息
*/
publicstaticvoidDelXML(HttpServletRequestrequest)throwsException{
FileInputStreamfi=null;
FileOutputStreamfo=null;
try{
Stringpath=request.getParameter("path");
intxmlid=Integer.parseInt(request.getParameter("id"));
fi=newFileInputStream(path);
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//得到根元素
Listbooks=root.getChildren();//得到根元素所有子元素的集合
books.remove(xmlid);//删除指定位置的子元素
Stringindent="";
booleannewLines=true;
XMLOutputteroutp=newXMLOutputter(indent,newLines,"GBK");
fo=newFileOutputStream(path);
outp.output(doc,fo);
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
/**
*添加XML文件指定信息
*/
publicstaticvoidAddXML(HttpServletRequestrequest)throwsException{
FileInputStreamfi=null;
FileOutputStreamfo=null;
try{
Stringpath=request.getParameter("path");
fi=newFileInputStream(path);
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//得到根元素
Listbooks=root.getChildren();//得到根元素所有子元素的集合
Stringbookname=request.getParameter("bookname");
Stringauthor=request.getParameter("author");
Stringprice=request.getParameter("price");
Stringpub=request.getParameter("pub");
Stringpubdate=request.getParameter("pubdate");
Textnewtext;
Elementnewbook=newElement("书");
Elementnewname=newElement("书名");
newname.setText(bookname);
newbook.addContent(newname);
Elementnewauthor=newElement("作者");
newauthor.setText(author);
newbook.addContent(newauthor);
Elementnewpub=newElement("出版社");
newpub.setText(pub);
newbook.addContent(newpub);
Elementnewprice=newElement("价格");
newprice.setText(price);
newbook.addContent(newprice);
Elementnewdate=newElement("出版日期");
newdate.setText(pubdate);
newbook.addContent(newdate);
books.add(newbook);//增加子元素
Stringindent="";
booleannewLines=true;
XMLOutputteroutp=newXMLOutputter(indent,newLines,"GBK");
fo=newFileOutputStream(path);
outp.output(doc,fo);
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
/**
*修改XML文件指定信息
*/
publicstaticvoidEditXML(HttpServletRequestrequest)throwsException{
FileInputStreamfi=null;
FileOutputStreamfo=null;
try{
Stringpath=request.getParameter("path");
intxmlid=Integer.parseInt(request.getParameter("id"));
fi=newFileInputStream(path);
SAXBuildersb=newSAXBuilder();
Documentdoc=sb.build(fi);
Elementroot=doc.getRootElement();//得到根元素
Listbooks=root.getChildren();//得到根元素所有子元素的集合
Elementbook=(Element)books.get(xmlid);
Stringbookname=request.getParameter("bookname");
Stringauthor=request.getParameter("author");
Stringprice=request.getParameter("price");
Stringpub=request.getParameter("pub");
Stringpubdate=request.getParameter("pubdate");
Textnewtext;
Elementnewname=book.getChild("书名");
newname.setText(bookname);//修改书名为新的书名
Elementnewauthor=book.getChild("作者");
newauthor.setText(author);
Elementnewpub=book.getChild("出版社");
newpub.setText(pub);
Elementnewprice=book.getChild("价格");
newprice.setText(price);
Elementnewdate=book.getChild("出版日期");
newdate.setText(pubdate);
//books.set(xmlid,book);//修改子元素
Stringindent="";
booleannewLines=true;
XMLOutputteroutp=newXMLOutputter(indent,newLines,"GBK");
fo=newFileOutputStream(path);
outp.output(doc,fo);
}
catch(Exceptione){
System.err.println(e+"error");
}
finally{
try{
fi.close();
fo.close();
}
catch(Exceptione){
e.printStackTrace();
}
}
}
}

下面是操作的jsp文件:
<%@pagecontentType="text/html;charset=GBK"%>
<%@pagelanguage="java"import="java.util.*,xml.*"%>
<html>
<head>
<title>读取XML文件资料</title>
</head>
<body>
<h3align="center">JDOM操作XML文件</h3>
<palign="center">读取XML文件中的所有资料</p>
<center>
<tableborder="1"cellpadding="0"cellspacing="1"style="border-collapse:collapse"width="80%"id="AutoNumber1">
<tr>
<tdalign="center"width="92">书名</td>
<tdalign="center"width="92">作者</td>
<tdalign="center"width="92">出版社</td>
<tdalign="center"width="92">价格</td>
<tdalign="center"width="92">出版日期</td>
<tdalign="center"width="94">操作</td>
</tr>
</table>
<%
Stringpath=application.getRealPath("/test/xml/")+"testC.xml";
XmlBeanxml=newXmlBean();
Vectorxmlall=xml.LoadXML(path);
for(inti=0;i<xmlall.size();i++){
xml=(XmlBean)xmlall.elementAt(i);
/**out.println("书名:"+xml.getbookname()+"<br>");
out.println("作者:"+xml.getauthor()+"<br>");
out.println("出版社:"+xml.getpub()+"<br>");
out.println("价格:"+xml.getprice()+"<br>");
out.println("出版日期:"+xml.getpubdate()+"<br><br>");
*/
%>
<tableborder="1"cellpadding="0"cellspacing="1"style="border-collapse:collapse"width="80%"id="AutoNumber2">
<tr>
<tdalign="center"width="92"><%=xml.getbookname()%></td>
<tdalign="center"width="92"><%=xml.getauthor()%></td>
<tdalign="center"width="92"><%=xml.getpub()%></td>
<tdalign="center"width="92"><%=xml.getprice()%></td>
<tdalign="center"width="92"><%=xml.getpubdate()%></td>
< tdalign="center"width="94"><ahref="xmlok.jsp?act=del&id=< %=i%>&path=<%=path%>">删除</a></td>
</tr>
</table>
<%}%>
</center>
<formmethod="POST"action="xmlok.jsp">
<palign="center">
<inputtype="radio"value="add"checkedname="act">添加资料<inputtype="radio"value="edit"name="act">编辑资料
序号:<selectsize="1"name="id">
<%for(inti=0;i<xmlall.size();i++){%>
<optionvalue="<%=i%>">第<%=i+1%>条</option>
<%}%>
</select><br>
书名:<inputtype="text"name="bookname"size="20"><br>
作者:<inputtype="text"name="author"size="20"><br>
出版社:<inputtype="text"name="pub"size="20"><br>
价格:<inputtype="text"name="price"size="20"><br>
日期:<inputtype="text"name="pubdate"size="20"></p>
<inputtype="hidden"name="path"value="<%=path%>">
<palign="center"><inputtype="submit"value="提交"name="B1"><inputtype="reset"value="重置"name="B2"></p>
</form>
</body>
</html>

下面是处理上一文件提交的jsp文件:
<%@pagecontentType="text/html;charset=GBK"%>
<%@pagelanguage="java"import="xml.*"%>
<%if(request.getParameter("act")!=null&&request.getParameter("act").equals("add")){
XmlBean.AddXML(request);
out.println("<palign='center'><br><br>添加成功<br><br><ahref=''>返回</a>");
}
elseif(request.getParameter("act")!=null&&request.getParameter("act").equals("del")){
XmlBean.DelXML(request);
out.println("<palign='center'><br><br>删除成功<br><br><ahref=''>返回</a>");
}
elseif(request.getParameter("act")!=null&&request.getParameter("act").equals("edit")){
XmlBean.EditXML(request);
out.println("<palign='center'><br><br>修改成功<br><br><ahref=''>返回</a>");
}
else{out.print("<palign='center'><br><br>非法操作<br><br><ahref=''>返回</a>");}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值