|
|
|
Last
revision of this document: |
This
step of the tutorial deals with reading a file that contains a
XML-structure.
A
Base-Class is developed that reads the XML-structure within a file
into a DOM-element (DOM = Document Objects Model; for detailed
information please see the link under 'Credits').
This base class
is later used within specialized classes to search the DOM-element
like a database.
Preface:Similar
parts of this tutorial were already documented with screenshots in
Base
lesson 2, step 3 - Read parameters for the connection to the
database.
To make this document shorter, similar
steps are not described in detail - just referenced to the mentioned
document.
Not to document these steps in detail is also intended
as a feedback:
you should be able to understand what the actions
are for and find the similarities for this project.
The
code written in this tutorial is far away from being
optimized.
Emphasis of this tutorial is to develop the application
in small steps where the completion of each step allows the
application to be run eror-free and showing the result aimed by the
step.
Therefore the code is written to be understandable in favor
of being optimized.
Credits:A
tutorial for reading a XML-structure in JAVA can be found under
http://java.sun.com/webservices/jaxp/dist/1.1/docs/tutorial/dom/index.html
.
JS_FC01e
- Universal structure for parameters and getting the directory-names
completed
- and its prerequisites too.
Recommended:
Structure
of the xml-file with language-dependant strings for the
user-interface read.
This
introduces the structure of the xml-tree with text-elements for the
GUI.
Base
lesson 2, step 3 - Read parameters for the connection to the
database implemented
or read at least
Preparation:Methods
for reading data out of XML-formatted structures are contained in the
external Java-ARchive named 'jdom.jar'.
How this JAR can be
downloaded and has to be incorporated into the 'Java Build Path' of
the project is described in Base
lesson 2, step 3 - Read parameters for the connection to the database
> Preparation.
Please
obey that the 'External JAR' must be integrated into the 'Build Path'
of project 'JS_FC01' and – if you decided to code the
base-classes by yourself (see JS_FC01ca –
Create a project for the base-classes) - in project 'JS_Base'
too.
Create
the Interface JSBS_XML-Constants:In
this Interface, control-values and status-code-values for all classes
handling XML-structures are defined.
An
Interface is created in the following way:
Right
click onto the project 'JS_Base' and select
>New>Interface.
Enter
the Package (js_base.xml),
the (Interface-)Name (JSBS_XML_Constants)
and click the [ Finish ] button.
Eclipse
has already generated a template and the individual code can be
entered.
For a listing of the code please refer to
Code
for JSBS_XML_Constants
.
Recommendation:
As typing the whole interface
might be boring now, just create the interface and type the
values whenever you get an error because a value is used and
does not exist by then.
Create
the class JSBS_XML_Base:This
class reads the file (for a file-name passed as parameter), checks if
the content of the file follows the XML-syntax and creates an element
with the XML-structure according to DOM (Document Object Model).
Right
click onto the project 'JS_Base' and select
>New>Class
Enter
the Package (js_base.xml),
the (Class-)Name (JSBS_XML_Base)
select (o) public,
do not check any boxes,
leave
the default Superclass (java.lang.Object),
[
Add ] Interfaces (JSBS_XML_Constants)
and click the [ Finish ]
button.
Eclipse
has already generated a template and the individual code can be
entered.
For a listing of the code please refer to Code
for JSBS_XML_Base.
Create
the class JSBS_XML_DisplayStrings:This
class is intended to have specialized methods for processing Objects
forming a GUI; particularly those of type JLabel, JText, JComboBox
and JListBox and setting their properties - mainly text for Labels
and ToolTipText.
For
now no methods are coded (except for the constructor).
This class
is introduced now that it can be incoporated into the
base-frame.
Methods for this class are coded in the next step(s).
Right
click onto the project 'JS_Base' and select
>New>Class
Enter
the Package (js_base.xml),
the (Class-)Name (JSBS_XML_DisplayStrings)
select (o) public,
do not check any boxes,
enter
the Superclass (JSBS_XML_Base)
and
click the [ Finish ] button.
Eclipse
has already generated a template and the individual code can be
entered.
For a listing of the code please refer to Code
for JSBS_XML_DisplayStrings.
For
now it is sufficient to code the Constructor /*
*
-------------------------------
* Constructor of the class
*/ public
JSBS_XML_DisplayStrings(JSBS_UniversalParameter
parmUniversalParameter) {.
. . . . . . . . }.
. . . . . . . .
Incorporate
the class JSBS_XML_DisplayStrings into the base-frame:The
structure will be used for a 'CommandCenter' of any application.
So
the structure is defined as a variable in the class
JSBS_StartFrame.package
public
classjs_base.frame;
import
javax.swing.*;
import
js_base.structures.*;
.
. . . . . . . . import
js_base.xml.*; .
. . . . . . . . JSBS_StartFrame
JFrame {/*
*
Structure with a variety of parameters;
* please see
description for eacht variable at the class of the structure.
*/public
JSBS_UniversalParameter
structJSBS_UniversalParameter; /*
*
XML-structure with properties for language dependant GUI-elements
(except JButton). */
public
JSBS_XML_DisplayStrings
structJSBS_XML_DisplayStrings;
'Construct'
the class JSBS_XML_DisplayStrings:As
explained for the classin the step before (
JSBS_UniversalParameter
JS_FC01e
- Universal structure for parameters and getting the
directory-names), for a better
understanding of the flow of code, the 'construction' of the class
is done in the project (JS_FC01).
First,
the packages containing the newly used class and the interface with
the status-codes have to be coded.package
js_errdb.clientframes;
import
java.awt.*; import
javax.swing.*;
import
js_base.frame.JSBS_StartFrame*;
import
js_base.structures.*;
import
js_base.xml.*; .
. . . . . . . .
. . . . . .
. . . public
class JS_ErrDB_CommandCenter extends
JSBS_StartFrame implements
JSBS_XML_Constants {
As
this class needs some variables from the class(directory, where the filename for
the file with the XML-structure is located), the construction of
this class is after the construction of
JSBS_UniversalParameter
JSBS_UniversalParameter..
. . . . . . . . .
. . . . . . . . private
void initialize_before_frame(String
parmstrLanguageCode) { structJSBS_UniversalParameter
/*
Initialize the structure with the set of parameters
*/=
new
JSBS_UniversalParameter(parmstrLanguageCode);/*
Verifiy if the class was constructed without an error. *//*
Initialize the structure with the XML-Element for language dependant
GUI-elements
* (except JButton).
*/ structJSBS_XML_DisplayStrings
=
new
JSBS_XML_DisplayStrings(structJSBS_UniversalParameter);
if
(structJSBS_XML_DisplayStrings.StatusCode
!= CONST_OK){
System.out.println(“Error
while building '+
(structJSBS_XML_DisplayStrings';
StatusCode: „ new
Integer(structJSBS_XML_DisplayStrings.StatusCode)).toString());
System.exit(structJSBS_XML_DisplayStrings.StatusCode);
}
private
void initialize_frame() {
setVisible(true);. . . . . . . . .
.
. . . . . . . .
Next
Step:As
the XML-structure with the language dependant strings are now in the
memory, we will develop a recursive method to set the text to the
elements of the GUI.
JS_FC01g
- Setting the properties of the GUI-elements.