struts开发实践—读写xml实例
2007-04-26 10:04
369 查看
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*********************************/
[align=left]<?xml version="1.0" encoding="UTF-8" ?> [/align]
<TBL_EMPLOYEE>
- <Field fieldName="employeeId" printName="职工ID">
<ifPrint>0</ifPrint>
</Field>
- <Field fieldName="employeeBasicInfo.deptName" printName="部门">
<ifPrint>1</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");
if (ifPrints.getLength() == 1) {
Element e = (Element) ifPrints.item(0);
Text t = (Text) e.getFirstChild();
t.setNodeValue("1");
}
}
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.i
4000
o.File(path));
transformer.transform(source, result);
}
/**
* 初始化xml
* @param path:the path of xml
* @throws Exception
*/
public void initialXMLFile(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 i = 0; i < fields.getLength(); i ) {
Element field = (Element) fields.item(i);
NodeList ifPrints = field.getElementsByTagName("ifPrint");
if (ifPrints.getLength() == 1) {
Element e = (Element) ifPrints.item(0);
Text t = (Text) e.getFirstChild();
t.setNodeValue("0");
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File(path));
transformer.transform(source, result);
}
}
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*********************************/
[align=left]<?xml version="1.0" encoding="UTF-8" ?> [/align]
<TBL_EMPLOYEE>
- <Field fieldName="employeeId" printName="职工ID">
<ifPrint>0</ifPrint>
</Field>
- <Field fieldName="employeeBasicInfo.deptName" printName="部门">
<ifPrint>1</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");
if (ifPrints.getLength() == 1) {
Element e = (Element) ifPrints.item(0);
Text t = (Text) e.getFirstChild();
t.setNodeValue("1");
}
}
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.i
4000
o.File(path));
transformer.transform(source, result);
}
/**
* 初始化xml
* @param path:the path of xml
* @throws Exception
*/
public void initialXMLFile(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 i = 0; i < fields.getLength(); i ) {
Element field = (Element) fields.item(i);
NodeList ifPrints = field.getElementsByTagName("ifPrint");
if (ifPrints.getLength() == 1) {
Element e = (Element) ifPrints.item(0);
Text t = (Text) e.getFirstChild();
t.setNodeValue("0");
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new java.io.File(path));
transformer.transform(source, result);
}
}
相关文章推荐
- struts开发实践—读写xml实例
- struts开发实践—读写xml实例(上)
- struts开发实践—读写xml实例(下)
- struts开发实践—读写xml实例
- struts开发实践—曲线图实例
- struts开发实践—柱形图实例
- struts开发实践—曲线图实例
- struts开发实践-柱形图、饼状图实例
- ajax/dwr/struts实例开发(下)
- Struts+Spring+Hibernate开发实例
- Android&nbsp;NFC开发读写NDEF数据实例
- struts开发实践—实用小贴士
- struts开发笔记--配置+简单实例
- [导入]XML for Analysis(XMLA)开发详解-(7)XMLA Execute方法解析及实例(完)
- WCF与AJAX编程开发实践(5):支持XML的Ajax WCF服务
- Struts 开发的最佳实践
- Struts+Spring+Hibernate开发实例
- Ajax_jquery_struts_json组合开发实例
- Struts2 快速入门(hello struts2 简单示例,配置开发模式,导入struts源码,设置struts.xml源码提示)
- 【开发技术】web.xml vs struts.xml