注册 | 登录
收藏 | 帮助
热门文章
编辑推荐
相关文章  
Norton AntiVirus 2005测试版截图
让Ghostbusters为电脑保驾护航
安全小知识:Rundll.exe是病毒吗?
Htran把内网肉鸡做成SOCKS5代理
Windows Vista系统防火墙初探
微软官方解读Vista安全性: 全面的
Windows利器:Rundll.exe高级应用
DNS (domain Name System) 系统的
DNS 系统设定例--7.[Fwd] 特殊网
IMail Anti-Virus简易说明及快速
您现在的位置: 顶尖设计 >> IT学院 >> 编程开发 >> Java >> 文章正文
struts开发实践—读写xml实例
作者:佚名  来源:不详  点击:  更新:2006-12-19
简介:
--文章源自一位网友


struts开发实践—读写xml实例 1.   准备工作:去java.sun.com下载org.w3c.dom包,将下载的包放在WEB-INF/lib目录下。包的文档说明可以参考http://java.sun.com/xml/jaxp/dist/1.1/docs/api/org/w3c/dom/package-summary.html。 2.   本案主要功能是完成table的表头显示字段的设置。 文件包括: 1) print.xml:将字段属性写入xml文件(放入web-inf目录下) 2) ReadWritePrintXML.java :print.xml的接口类: 3) PrintReadAction.java :print.xml的Action 4)PrintSetAction.java:写print.xml的Action 5) PrintForm:print选择的对象ActionForm   /****************print.xml代码begin******************************************/ <?xml version="1.0" encoding="UTF-8" ?> <TBL_EMPLOYEE> - <Field fieldName="employeeId" printName="职工ID">   <ifPrint>0</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.deptName" printName="部门">   <ifPrint>1</ifPrint>   </Field> <Field fieldName="employeeBasicInfo.employeeName" printName="职工姓名">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.sex" printName="性别">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.nation" printName="民族">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.birthDate" printName="出生日期">   <ifPrint>0</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.ages" printName="年龄">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.workYear" printName="工龄">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.idCard" printName="身份证号">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.workDate" printName="参加工作时间">   <ifPrint>0</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.politic" printName="政治面貌">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="employeeBasicInfo.parchment" printName="文化程度">   <ifPrint>1</ifPrint>   </Field> - <Field fieldName="nativePlace" printName="籍贯">   <ifPrint>0</ifPrint>   </Field> - <Field fieldName="registerPlace" printName="户口所在地">   <ifPrint>0</ifPrint>   </Field> - <Field fieldName="telephone" printName="联系电话">   <ifPrint>0</ifPrint>   </Field> - <Field fieldName="houseAddress" printName="家庭住址">   <ifPrint>0</ifPrint>   </Field>   </TBL_EMPLOYEE> /**********************print.xml代码end***************************************/ /**********************PrintForm.java Begin***************************************/ package test;   import org.apache.struts.action.*; import javax.servlet.http.*; /**  * 打印form  */ public class PrintForm extends ActionForm {   /**字段名*/   private String fieldName="";   /**打印名*/   private String printName="";   /**是否打印*/   private int ifPrint=0;   public void setFieldName(String fieldName) {     this.fieldName = fieldName;   }   public String getFieldName() {     return fieldName;   }   public void setPrintName(String printName) {     this.printName = printName;   }   public String getPrintName() {     return printName;   }   public void setIfPrint(int ifPrint) {      this.ifPrint = ifPrint;    }    public int getIfPrint() {      return ifPrint;    }     public ActionErrors validate(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {     /**@todo: finish this method, this is just the skeleton.*/     return null;   }   public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {   } } /****************printForm.java代码end******************************/   /****************ReadWritePrintXML.java代码Begin******************************/ package test;   import java.io.*; import java.util.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.apache.crimson.tree.XmlDocument; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; /**  * 读写printxml  */ public class ReadWritePrintXML {   /**    * 读printxml    * @param path:the path of xml put in    * @return    * @throws Exception    */   public ArrayList readXMLFile(String path) throws Exception {     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();     DocumentBuilder db = null;     ArrayList list = new ArrayList();     String[] data = null;     try {       db = dbf.newDocumentBuilder();     }     catch (ParserConfigurationException pce) {       pce.printStackTrace();     }     Document doc = null;     try {       doc = db.parse(path);     }     catch (DOMException dom) {       dom.printStackTrace();     }     catch (IOException ioe) {       ioe.printStackTrace();     }     Element root = doc.getDocumentElement();     NodeList fields = root.getElementsByTagName("Field");     for (int i = 0; i < fields.getLength(); i++) {       Element field = (Element) fields.item(i);       PrintSelectForm printSelectForm = new PrintSelectForm();       printSelectForm.setFieldName(field.getAttribute("fieldName"));       printSelectForm.setPrintName(field.getAttribute("printName"));       NodeList datas = field.getElementsByTagName("ifPrint");       if (datas.getLength() == 1) {         Element e = (Element) datas.item(0);         Text t = (Text) e.getFirstChild();         printSelectForm.setIfPrint(t.getNodeValue());       }       list.add(printSelectForm);     }     return list;   }     /**    * 写xml    * @param select:the selected id you set    * @param path:the path of xml put in    * @throws Exception    */   public void writeXMLFile(String[] select, String path) throws Exception {     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();     DocumentBuilder db = null;     try {       db = dbf.newDocumentBuilder();     }     catch (ParserConfigurationException pce) {       pce.printStackTrace();     }     Document doc = null;     try {       doc = db.parse(path);     }     catch (DOMException dom) {       dom.printStackTrace();     }     catch (IOException ioe) {       ioe.printStackTrace();     }       Element root = doc.getDocumentElement();     NodeList fields = root.getElementsByTagName("Field");     for (int j = 0; j < select.length; j++) {       for (int i = 0; i < fields.getLength(); i++) {         Element field = (Element) fields.item(i);         if (field.getAttribute("fieldName").equals(select[j])) {           NodeList ifPrints = field.getElementsByTagName("ifPrint");






  • 上一篇文章:
  • 下一篇文章:
  • 分享此文:该页面添加到 Mister Wong 添加到雅虎Yahoo!收藏 Add to:Del.icio.us Post to Furl Digg this 添加到Google书签 reddit spurl blogmarks 365Key 评论  收藏  分享  打印
     我来说两句
    姓名:       验证码:   
    主页: 
    评分: 1分 2分 3分 4分 5分
    本频道近期热评文章:
      关于我们 | 联系我们 | 站点地图 | 广告投放 | 友情链接 | 在线留言 | 版权申明
    版权所有 © 2004-2007 顶尖设计(bobd.cn)
    未经授权禁止转载,摘编,复制本站内容或建立镜像. 沪ICP备07504942号 
    网络110
    报警服务