/*** 写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.io.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);}}/****************ReadWritePrintXML.java代码End********************************//****************PrintReadAction.java代码Begin***********************************/package test;import org.apache.struts.action.*;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.ServletException;import java.io.IOException;/*** 读取print.xml的字段信息,供选择*/public class PrintReadAction extends Action {public ActionForward perform(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException {ActionErrors errors = new ActionErrors();String[] ifPrint=null;String path=request.getRealPath("/")+"/WEB-INF/print.xml";try {ReadWritePrintXML readwrite=new ReadWritePrintXML();Collection colField = readwrite.readXMLFile(path);request.setAttribute(BeanNames.TABLEFIELD_LIST, colField);return mapping.findForward("success");}catch (Throwable e) {e.printStackTrace();ActionError error = new ActionError(e.getMessage());errors.add(ActionErrors.GLOBAL_ERROR, error);}saveErrors(request,errors);return new ActionForward(mapping.getInput());}}/****************PrintReadAction.java代码End**********************************//****************PrintSetAction.java代码Begin*********************************/package test;import org.apache.struts.action.*;import javax.servlet.http.*;import javax.servlet.*;import java.io.*;import java.util.*;/*** 打印字段设置回写print.xml文件*/public class PrintSetAction extends Action {public ActionForward perform(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{ActionErrors errors = new ActionErrors();String[] multiSelect=request.getParameterValues("multiSelect");ArrayList colField=null;String path=request.getRealPath("/")+"/WEB-INF/print.xml";try{ReadWritePrintXML readwrite = new ReadWritePrintXML();readwrite.initialXMLFile(path);readwrite.writeXMLFile(multiSelect,path);return null;}catch (Throwable e) {e.printStackTrace();ActionError error = new ActionError(e.getMessage());errors.add(ActionErrors.GLOBAL_ERROR, error);}saveErrors(request, errors);return new ActionForward(mapping.getInput());}}/****************PrintSetAction.java代码End**********************************/