Article originates from a netizen
This article is written by smilelily, yesterday, I found my own email record, he has users on 9cbs, articles source address http://dev.9cbs.net/User/smilelily
Struts Development Practice - Read and write XML instance
1. Preparation: Go to java.sun.com to download the org.w3c.dom package, put the downloaded package in the web-inf / lib directory. The documentation of the package can be referred to http://java.sun.com/xml/jaxp/dist/1.1/docs/api/org/w3c/dom/package-summary.html.
2. The main function of this case is to complete the setting of the table of Table.
Documents include:
1) Print.xml: Write field properties into XML files (put into web-inflicity)
2) READWRITEPRINTXML.JAVA: Print.xml Interface Class:
3) PrintReadAction.java: Print.xml action
4) PrintSetAction.java: Write Print.xml Action
5) Printform: Print Select Object Actionform
/****************print.xml codes begin **************************** ************* /
XML Version = "1.0" encoding = "UTF-8"?>
-
Field>
-
Field>
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
-
Field>
TBL_EMPLOYEE>
/ ********************************************************* ***************** /
/************PRINTFORM.JAVA BEGIN ********************************* **************** /
Package test;
Import org.apache.struts.Apache.struts.action. *;
Import javax.servlet.http. *;
/ **
* Print FORM
* /
Public class printform extends actionform {
/ ** field name * /
Private string fieldname = ""; / ** Printed name * /
Private string printname = "";
/ ** Is printing * /
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 Code end **************************** ** /
/******ReadwriteprintXML.java code 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;
/ **
* Read and write PrintXML
* /
Public class readwriteprintXML {
/ **
* Read 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 Element Field = (Element) Fields.Item (i); PrintSelectForm PrintSelectform = new printselectform (); PrintSelectform.SetfieldName (Field.GetaTribute ("FieldName")); PrintSelectform.SetPrintName (Field.getaTribute ("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; } / ** * Write XML * @Param Select: The success 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 For (int i = 0; i Element Field = (Element) Fields.Item (i); if (Field.getaTribute ("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); } / ** * Initialization 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 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 code end **************************************** **** / /****************PrintReadAction.java Code Begin **************************** ******* / Package test; Import org.apache.struts.Apache.struts.action. *; Import javax.servlet.http.httpservletRequest; Import javax.servlet.http.httpservletResponse; Import javax.servlet.servletException; Import java.io.ioException; / ** * Read the field information of Print.xml for selection * / Public class printreadaction extends action { Public ActionForward Perform (ActionMApping Mapping, Actionform Form, HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE RESPONSE THROWS IOException, servletexception { ActionerroS 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.setttribute (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 Code End **************************** ****** / /****************PrintSetAction.java Code Begin **************************** ***** / Package test; Import org.apache.struts.Apache.struts.action. *; Import javax.servlet.http. *; Import javax.servlet. *; Import java.io. *; Import java.util. *; / ** * Print field setting back write print.xml file * / Public class printsetAction extends action { Public ActionForward Perform (ActionMapping Mapping, Actionform Actionform, HttpservletRequest Request, HttpservletResponse Response) Throws servletexception, ioException { ActionerroS 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 Code End **************************** ****** /