Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Object-Serialization in a xml-String (!) in Java

0 views
Skip to first unread message

Melanie Brade

unread,
Dec 20, 2002, 5:40:03 AM12/20/02
to
Hi all,

i'm working with Java and XML. Now i want to serialize an object tree
(or for beginning a single object) in a xml-string. Therefor i need to
konvert my object into a xml-file and then serialize. But how?
Has anybody some experience in that.

Merry X-Mas greetings

Mel

Lothar Schwab

unread,
Dec 22, 2002, 12:35:43 PM12/22/02
to
The most straight forward method I can think of is to:
1. use an XML parser to create an XML document.
2. Use the XML DOM functions to add nodes and XML attributes according to
your object or objects
3. Receive a string representation from the parser.

Creating the object(s) back from the string work similar:
1. Tell the XML parser to create the XML document from the string.
2. Naviagate through the XML tree and recreate the object(s).

The difficult task is to find the right mapping between the object(s) and
their attributes and the XML tree. The easiest case is obviously if only one
object needs to be serialized and if this one object has only "atomic"
attributes (means no references to other objects).

In this case the attributes of the objects can be mapped to XML attributes.
The value of the object attribute must be mapped into a string
representation (because XML attributes can only have string values).

It becomes trickier if you want to serialize an object which contains
references to further objects. A pretty common pattern to do this works like
this:
1. There is a scanning algorithm which navigates to all "reachable" objects.
This means that all objects which are directly or indirectly reachable from
your root objects are being "registered" in a "list". This is a process
similar to the mathematical concept of a "transitive hull". Depending on
your application there may or may not be cyclic references or slings. If
this is the case the scanning algorithm must be able to handle these cases.
2. Now your algorithm goes through the object network again and mapping each
object into XML constructs (nodes and attributes). An object reference needs
to be represented with a key of the "registered object" list so that the
deserialization can make sure that multiple references to the same object
are correctly recreated.

Java supports meta information. This means that the algorithms specified
above can even be implemented in a generic way (they are not specifically
developed for a particular application but they work for every application.

Note that serialization does only allows you to "freeze" and "restore" the
state of an application in certain limited cases. It is usually sufficient
for "document oriented" applications. Such applications load a certain data
set into memory. Then the user can modify it and explicitly save it. The
serialization usually works great for such cases.

Note that some (especially technical) applications don't allow for
serialization because of multiple reasons:
- The data set can be so huge that it does not fit into main memory all at
once. In this case the applications are usually written as stateless
database applications which are kicked off - then they read some data from
the database via a bulk interface, modify it and write it back.
- Applications which entertain communications channels to other applications
(or devices) need to get these connections back. Serialization can usually
not take care of this.
- Serialization can impose big performance problems. Note that the XML
representation of an object is usually much larger than the object itself in
memory. Also note that the performance of mapping to XML and also the
performance of the XML parser can easily bring the performance of the whole
application down to an unacceptable level. The graph algorithms for building
a transitive hull have overlinear time behavior. This disqualifies
serialization for many high performance applications which need to deal with
bulk data.

LS


"Melanie Brade" <mab...@web.de> wrote in message
news:4baf329d.02122...@posting.google.com...


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----

0 new messages