XeriL |
Introduction |
.ser
" file, which can then be stored, or sent through the
network... The problem with this approach is that the serialized object
is not human readable and thus cannot be edited by hand or even read. We
would like to produce something that is human readable and
editable. This can then be used for configuration files for example.
The format of the file |
public class MyTest { private int myIntField; private double myDoubleField; private java.util.Date myDateField; } |
<MyTest> <myIntField>13</myIntField> <myDoubleField>34.98</myDoubleField> <myDateField>07/20/2000</myDateField> </MyTest> |
<!ELEMENT null EMPTY > <!ELEMENT myDateField (#PCDATA)> <!ELEMENT myDoubleField (#PCDATA)> <!ELEMENT MyTest (myIntField, myDoubleField, myDateField)> <!ELEMENT myIntField (#PCDATA)> |
Date
"
object by a simple string (this is called customization).
How to use it |
ObjectToXml
that turns an object into an XML output
using the static method ObjectToXml.objectToXml(Object o,
PrintWriter out)
XmlToObject
that turns an XML input into an object
using the static method XmlToObject.xmlToObject(InputStream
in, Class cl)
ClassToDtd
. You
invoke it like this: 'java org.xeril.xml.ClassToDtd
xxxxx
' where xxxx
is the name of
the class (including the package information). Please note that you can
'tweak' the DTD by hand afterwards (for example, adding
'?
' if a parameter is optional).
Customization |
XmlInfo
interface.XmlInfo
(ex: if
you want to customize test.MyTest
then you have to name
the class test.MyTestXmlInfo
). An extension to this, in
order not to pollute the global namespace with this kind of class, is
to define an XmlInfo
class which is nested within the
original class (thus the name of that new class would be
test.MyTest$XmlInfo
)SimpleXmlInfo
class which implements the XmlInfo
interface
and provides some convenient methods.
XmlInfo
|
XmlInfo
interface
holds two methods.
getXmlName()
:
returns the xml tag name of this class (if you don't provide it then
the class name will be taken instead).getXmlMapDescriptors()
:
returns an array of XmlMapDescriptor
objects. Each one of this object allows you to specify what you want
to do with a field (you don't have to provide one for each field...
because if not defined then default value will be taken, for example
the name of the tag will be the name of the field).
XmlMapDescriptor
|
XmlMapDescriptor
allows you to customize one field:
_xxx
, it doesn't look nice
to have an XML tag called <_xxxx>
).XmlMapper
to
it.
XmlMapper
|
XmlMapper
is
another interface that maps an object to a string representation and
vice-versa. This is extremely powerfull and can be extended at
will. This package provides 2 'standard' mapper classes and it is easy
to write your own:
XmlMapperDate
for converting a java.util.Date
object into a nice
String representation.XmlMapperClass
for converting a java.lang.Class
object into just the
class name.Example |
Ex1
defines 2 simple types, an array of double, another object and a
Date
. It contains the nested XmlInfo
class
that customizes its xml behavior.Ex2
defines a
hidden field, a simple type and an array of Class
objects. It contains the nested XmlInfo
class
that customizes its xml behavior.Test
program simply
reads an xml file that represents an Ex1
object and
redisplays it as its output after calling the change method on it.ex.xml
Constraints, restrictions and future developments |
java.lang.reflect.Field.setAccessible(true)
which will
throw a security exception if you don't have the right property
file.Dependencies and requirements |
java.lang.reflect.Field.setAccessible(true)
method call,
introduced with JDK1.2.
javax.xml.parsers
,
org.w3c.dom
and org.xml.sax
. They can be
found in JAXP (the Java API
for XML Parsing) provided by SUN. Those packages are actually part
of JDK1.4, so it is no longer necessary to download them
separately.
License and Copyright |
Download |
Source |
cvs.xeril.sourceforge.net:/cvsroot/xeril
and the directory
name is XeriL