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. Documentation of the package Description: 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 code begin ***************************************** /
XML Version = "1.0" encoding = "UTF-8"?>
-
Field>
-
Field>
............ / / omitted
TBL_EMPLOYEE>
/ ****************************************************************************** * /
/*****************Printform.java begin ******************************** /
Package test;
Import org.apache.struts.action. *;
Import javax.servlet.http. *;
/ ** * Print Form * /
Public class printform extends actionform {
/ ** field name * /
Private string fieldname = ""
/ ** Print 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 **************************** ** /
/******************************************************************** /
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 ("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 ("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); } }