在我工作过程中,遇到了对临时存储产品信息的XML文件进行操作的问题.其中就有对XML文件的解析操作,考虑到用DOM或SAX比较麻烦,于是我选择了用jdom进行解析.因为我的XML文件结构比较简单,仅有两层,而且没有复杂的属性,所以没有用到里面太多的方法,只希望能够抛砖引玉,给初学者一点帮助.
下面我就把大概的实现过程说一说.
一.实现解析xml文件的JavaBean(XMLBean):
我把对存放产品信息的xml文档的全部操作都写在了XMLBean()里面,包括添加,修改,删除一条记录,查看相关记录等操作.具体实现的代码如下:
package xml;
import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.output.*;
import org.jdom.input.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* <p>Title:XMLBean</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* @author lihs
* @version 1.0
**/
/*
**
**通过往XML文件里面添加,删除,修改记录。从而实现对生产部门提交产品信息的处理。
*/
public class XMLBean{
private String ProduceID,ProduceName,ProduceClass,ProduceType,ProduceColor,Baozhiqi,ProduceNum,ProduceDep,ProduceDate;
public String getProduceID() { return ProduceID;}
public String getProduceName() { return ProduceName;}
public String getProduceClass() { return ProduceClass;}
public String getProduceType() { return ProduceType;}
public String getProduceColor() { return ProduceColor;}
public String getBaozhiqi() { return Baozhiqi;}
public String getProduceNum() { return ProduceNum;}
public String getProduceDep() { return ProduceDep;}
public String getProduceDate() { return ProduceDate;}
public void setProduceID(String produceid) { this.ProduceID =produceid ; }
public void setProduceName(String producename) { this.ProduceName =producename; }
public void setProduceClass(String produceclass) { this.ProduceClass =produceclass ; }
public void setProduceType(String producetype) { this.ProduceType =producetype ; }
public void setProduceColor(String producecolor) { this.ProduceColor =producecolor ; }
public void setBaozhiqi(String baozhiqi) { this.Baozhiqi =baozhiqi ; }
public void setProduceNum(String producenum) { this.ProduceNum =producenum ; }
public void setProduceDep(String producedep) { this.ProduceDep =producedep ; }
public void setProduceDate(String producedate) { this.ProduceDate =producedate ; }
public XMLBean(){}
/**
* 通过传入路径读取XML文件的内容。
*/
public Vector LoadXML(String path)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 produces = root.getChildren(); //获取根节点下面的所有子元素
Element produce =null;
XMLBean xml =null;
for(int i=0;i<produces.size();i++){
xml = new XMLBean();
produce = (Element)produces.get(i ); //取得指定的孩子节点信息
xml.setProduceID(produce.getChild("ProduceID").getText());
xml.setProduceName(produce.getChild("ProduceName").getText());
xml.setProduceClass(produce.getChild("ProduceClass").getText());
xml.setProduceType(produce.getChild("ProduceType").getText());
xml.setProduceColor(produce.getChild("ProduceColor").getText());
xml.setBaozhiqi(produce.getChild("Baozhiqi").getText());
xml.setProduceNum(produce.getChild("ProduceNum").getText());
xml.setProduceDep(produce.getChild("ProduceDep").getText());
xml.setProduceDate(produce.getChild("ProduceDate").getText());
xmlVector.add(xml);
}
}
catch(Exception e){
System.err.println(e+"error");
}
finally{
try{
fi.close();
}
catch(Exception e){
e.printStackTrace();
}
}
return xmlVector;
}
/**
* 删除指定的元素信息
*/
public static void DelXML(HttpServletRequest request)throws Exception{
FileInputStream fi = null;
FileOutputStream fo = null;
try{
String path=request.getParameter("path");
int xmlid=Integer.parseInt(request.getParameter("id"));
fi = new FileInputStream(path);
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(fi);
Element root = doc.getRootElement();
List produces = root.getChildren();
produces.remove(xmlid);