/**********************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******************************/

解决方案 »

  1.   

    /****************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.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********************************/
      

  2.   

    不用那么复杂吧,去下载jdom包导入,里面提供读取的sample,而且才几行代码,效率超高