--文章源自一位网友
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
- PrintForm:print选择的对象ActionForm
/print.xml代码begin**************************/
1<tbl_employee>
2
3**_-_ ** <field fieldname=" **employeeId** " printname=" **职工** **ID** ">
4
5**** <ifprint> **0** </ifprint>
6
7**** </field>
8
9**_-_ ** <field fieldname=" **employeeBasicInfo.deptName** " printname=" **部门** ">
10
11**** <ifprint> **1** </ifprint>
12
13**** </field>
14<field fieldname=" **employeeBasicInfo.employeeName** " printname=" **职工姓名** ">
15
16**** <ifprint> **1** </ifprint>
17
18**** </field>
19
20**_-_ ** <field fieldname=" **employeeBasicInfo.sex** " printname=" **性别** ">
21
22**** <ifprint> **1** </ifprint>
23
24**** </field>
25
26**_-_ ** <field fieldname=" **employeeBasicInfo.nation** " printname=" **民族** ">
27
28**** <ifprint> **1** </ifprint>
29
30**** </field>
31
32**_-_ ** <field fieldname=" **employeeBasicInfo.birthDate** " printname=" **出生日期** ">
33
34**** <ifprint> **0** </ifprint>
35
36**** </field>
37
38**_-_ ** <field fieldname=" **employeeBasicInfo.ages** " printname=" **年龄** ">
39
40**** <ifprint> **1** </ifprint>
41
42**** </field>
43
44**_-_ ** <field fieldname=" **employeeBasicInfo.workYear** " printname=" **工龄** ">
45
46**** <ifprint> **1** </ifprint>
47
48**** </field>
49
50**_-_ ** <field fieldname=" **employeeBasicInfo.idCard** " printname=" **身份证号** ">
51
52**** <ifprint> **1** </ifprint>
53
54**** </field>
55
56**_-_ ** <field fieldname=" **employeeBasicInfo.workDate** " printname=" **参加工作时间** ">
57
58**** <ifprint> **0** </ifprint>
59
60**** </field>
61
62**_-_ ** <field fieldname=" **employeeBasicInfo.politic** " printname=" **政治面貌** ">
63
64**** <ifprint> **1** </ifprint>
65
66**** </field>
67
68**_-_ ** <field fieldname=" **employeeBasicInfo.parchment** " printname=" **文化程度** ">
69
70**** <ifprint> **1** </ifprint>
71
72**** </field>
73
74**_-_ ** <field fieldname=" **nativePlace** " printname=" **籍贯** ">
75
76**** <ifprint> **0** </ifprint>
77
78**** </field>
79
80**_-_ ** <field fieldname=" **registerPlace** " printname=" **户口所在地** ">
81
82**** <ifprint> **0** </ifprint>
83
84**** </field>
85
86**_-_ ** <field fieldname=" **telephone** " printname=" **联系电话** ">
87
88**** <ifprint> **0** </ifprint>
89
90**** </field>
91
92**_-_ ** <field fieldname=" **houseAddress** " printname=" **家庭住址** ">
93
94**** <ifprint> **0** </ifprint>
95
96**** </field>
97
98**** </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");