Our project uses XML to pass data between layers. We don't want to create any physical xml files. The approach what we are using currently is xml as a String object (The whole XML will be a java.lang.String object with tags). Now we are planning to migrate to Document object instead of String.
Which is the better approach, if we have cater to multiple channels?
-
XML between layers (15 messages)
- Posted by: Dalia Reji
- Posted on: June 27 2001 06:40 EDT
Threaded Messages (15)
- XML between layers by Giedrius Trumpickas on June 27 2001 15:36 EDT
- XML between layers by Tony Brookes on June 27 2001 16:48 EDT
-
XML between layers by Giedrius Trumpickas on June 28 2001 05:56 EDT
- XML between layers by Tony Brookes on June 29 2001 12:08 EDT
-
XML between layers by Giedrius Trumpickas on June 28 2001 05:56 EDT
- XML between layers by Tony Brookes on June 27 2001 16:48 EDT
- XML between layers by Wojciech Ozimek on June 29 2001 03:44 EDT
- XML between layers by Tony Brookes on June 29 2001 09:20 EDT
-
XML between layers by Wojciech Ozimek on July 01 2001 06:08 EDT
-
XML between layers by Dalia Reji on July 02 2001 06:04 EDT
-
XML between layers by Tony Brookes on July 02 2001 11:09 EDT
-
XML between layers by Dalia Reji on July 03 2001 07:35 EDT
- XML between layers by Tony Brookes on July 05 2001 07:20 EDT
-
XML between layers by Dalia Reji on July 03 2001 07:35 EDT
- XML between layers by madhav bkr on July 08 2001 05:30 EDT
-
XML between layers by Tony Brookes on July 02 2001 11:09 EDT
-
XML between layers by Tony Brookes on July 02 2001 11:09 EDT
- XML between layers by Wojciech Ozimek on July 03 2001 12:51 EDT
-
XML between layers by Dalia Reji on July 02 2001 06:04 EDT
-
XML between layers by Wojciech Ozimek on July 01 2001 06:08 EDT
- XML between layers by Tony Brookes on June 29 2001 09:20 EDT
- XML between layers by madhav bkr on January 23 2002 23:11 EST
-
XML between layers[ Go to top ]
- Posted by: Giedrius Trumpickas
- Posted on: June 27 2001 15:36 EDT
- in response to Dalia Reji
With Document object you will have huge serialization orverhead. Because each node of document will serialized separatly. With String you don't have very little serialization overhead, but it looks weird -). -
XML between layers[ Go to top ]
- Posted by: Tony Brookes
- Posted on: June 27 2001 16:48 EDT
- in response to Giedrius Trumpickas
That's not a complete answer.
It's a trade off. A String will probably serialize faster than a Document object. But, if you send a String over the wire, then you probably have to re-parse it back into a Document object at the other end, which takes even more time.
In general, pass the parsed object around, but try not to pass huge trees around (you will probably blow up your stack if you do that, since most implementation use recursion to serialize their trees.
I would caution you strongly to be very very sure you want to use XML for this kind of messaging, it could cripple your performance.
You might be better off passing things between layer in a more efficient format and simply persisting it in XML. That tends to perform much better.
Hope that helps
Chz
Tony -
XML between layers[ Go to top ]
- Posted by: Giedrius Trumpickas
- Posted on: June 28 2001 05:56 EDT
- in response to Tony Brookes
This is a complete answer because this person mentioned that they alreay using XML as interchange format.
Blow up stack lol? As I understand they not planing to pass 2gb trees. They jus passing simple entities.
And I dont thin that for them now is easy to switch from XML to simple plain java objects.
And btw "You might be better off passing things between layer in a more efficient format and simply persisting it in XML" sounds very informative. This senetence does not give any information just bunch of buzzwords persistence, XML. Why he should pertsist entities as XML if he already using RDBMS ? =) -
XML between layers[ Go to top ]
- Posted by: Tony Brookes
- Posted on: June 29 2001 00:08 EDT
- in response to Giedrius Trumpickas
I wasn't trying to in any way put down your comment, if that's how you took it then please accept my apologies.
However, since I have built systems of the nature being described I am more than happy to elaborate...
"You might be better off passing things between layer in a more efficient format and simply persisting it in XML"
The point here is that you might want to consider passing your data around in object form, what I typically refer to as the volatile format. In other words it doesn't survive a server crash. However, you still have the ability to restore the systems state by pulling the state back in from it's persistent form, where XML would perhaps make sense.
I do not believe it is always advisable to use XML between layers of a system. As your interface into the system from the outside, absolutely, within your system, not convinced. Parsing XML is highly expensive in terms of CPU usage and if the layers of your system are on different boxes then XML is a rather bulky format which increases your usage of the network as well.
However, the point of the original question was not "XML or not XML" it was "String or org.w3c.dom.Document" and I stand by my comment about blowing the stack. Whether the DOM you pass round be very wide or very deep the eventual recursive calls to the same serializeChild(...) or similar method DOES eventually blow the stack, I've seen it (at least with Xerces.) The default stack size is, I believe 128K and it's actually not at all that hard to blow that if your XML get's too large. I had to increase stack size to 256K which gets round the problem. I also stand by my comment that the overhead of re-parsing the String into a Document object will probably be much greater than the cost of serializing a Document object rather than a String.
In actuality, if the nature of the client is unknown then it's easy to Facade this so that it doesn't matter now you do it. These are just adapters in the standard Adapter pattern.
You can also serialize in another way, using XML serialization, which creates the String representation from the Document object, into an output stream, which is a good way of throwing XML over a socket to a non Java client.
OK, think that covers it pretty thorougly.
Chz
Tony -
XML between layers[ Go to top ]
- Posted by: Wojciech Ozimek
- Posted on: June 29 2001 03:44 EDT
- in response to Dalia Reji
We are currently using hashtable for sending XML between layers. Hashtable contains string with XML being sent and some data allowing us to quickly change the state of operation (success/failure), exceptions, numerical data etc. We considered both methods described by you, and decided to use something "in the middle".
Consider, that what you need for the multichannel architecture is:
a\ XML (to be processed by XSLT - in our case) - display
b\ Some data for controlling navigation/flow - interaction
First, we were afraid, that the number of additional data in the hashtable will be too big. During the design, we've determined - that flow-info isn't very complex, and can be presented in a very smart way.
Regards
Wojtek -
XML between layers[ Go to top ]
- Posted by: Tony Brookes
- Posted on: June 29 2001 09:20 EDT
- in response to Wojciech Ozimek
Tiny point, but Hashtable or HashMap?
Unless you're sharing the thing between threads you may find HashMap a lot faster since none of it's methods are synchronized.
Chz
Tony
PS. Wrapping the XML with some additional state also makes good sense. JMS works the same way, using the message header and body. Your concept is the same. -
XML between layers[ Go to top ]
- Posted by: Wojciech Ozimek
- Posted on: July 01 2001 06:08 EDT
- in response to Tony Brookes
Good point. I must think about it a little. The only synchronization problem I can imagine, can occure at the web-app container side.
Thanx
Wojtek -
XML between layers[ Go to top ]
- Posted by: Dalia Reji
- Posted on: July 02 2001 06:04 EDT
- in response to Wojciech Ozimek
Hi,
I have one more question, how can we compare the performance of String and Document if I have to use them with EJBs, I mean which object performs better when serialized?
-
XML between layers[ Go to top ]
- Posted by: Tony Brookes
- Posted on: July 02 2001 11:09 EDT
- in response to Dalia Reji
That really depends on how big the XML is.
The best thing you can do is run some tests, with fixed XML documents of the size you plan on dealing with. Something like this:
long start = System.currentTimeMillis();
Document doc = remoteObject.getDocument();
long end = System.currentTimeMillis();
System.out.println("Took '" + end-start + "' ms to get object using Document object.");
start = System.currentTimeMillis();
String str = remoteObject.getDocumentString(); // Make sure this works in the same way your remote end would generate the String.
long string = start - System.currentTimeMillis();
start = System.currentTimeMillis();
Document doc = <use parser to convert str into Document>;
end = System.currentTimeMillis();
System.out.println("Time to get String was '" + string + "' ms");
System.out.println("Time to parse into Document was '" + end-start + "' ms");
That ought to give you at least an order of magnitude. There are countless other factors to consider, but if you make sure your Document objects and String objects are fixed in size (So you have something constant to measure across) then you can get an idea for the cost of serialization versus the cost of parsing.
Hope that helps
Chz
Tony -
XML between layers[ Go to top ]
- Posted by: Dalia Reji
- Posted on: July 03 2001 07:35 EDT
- in response to Tony Brookes
Hi,
We indeed collected the timestamps for building the XML and parsing it using both the approaches. Document takes less time compared to String (difference is almost a minute for 1000 records with 5 fields).
Does SAX parser be of any help compared to DOM parser. ie, let the XML be still held in String object, but use SAX parser to parse it? -
XML between layers[ Go to top ]
- Posted by: Tony Brookes
- Posted on: July 05 2001 19:20 EDT
- in response to Dalia Reji
The SAX parser is less memory intensive, and could be faster. Depends on how you implement your callback method really. You might decide to throw away tags you don't need, which would speed things up for sure.
In general, if the DOM is small, then a DOM parser is as fast as any of the alternatives, apart from direct parsing which I assume is not desirable or you would probably not bother using XML in the first place. :-)
Chz
Tony
PS. Glad it's quicker with Document. Makes sense. Serializing an object graph is much quicker than parsing. Apart from anything else a Document object is a big graph and the parse operation creates a ton of objects, all of which end up on the heap. In a multi-threaded environment thats a performance penalty, since only one thread can create an object on the heap at a time.
Chz
Tony -
XML between layers[ Go to top ]
- Posted by: madhav bkr
- Posted on: July 08 2001 05:30 EDT
- in response to Dalia Reji
Hi,
instead of usinfg a string object to pass xml doc you can use Jdom parser and very well use document object.
It's quite possible.
madhav -
XML between layers[ Go to top ]
- Posted by: Tony Brookes
- Posted on: July 02 2001 11:09 EDT
- in response to Wojciech Ozimek
If it's just your code then the container won't touch it and won't manipulate the HashMap so you don't have to worry about it.
Chz
Tony -
XML between layers[ Go to top ]
- Posted by: Wojciech Ozimek
- Posted on: July 03 2001 12:51 EDT
- in response to Tony Brookes
Thanks again. I've talk about it with my crew. Seems like we need some refactoring.
Regards
Wojtek -
XML between layers[ Go to top ]
- Posted by: madhav bkr
- Posted on: January 23 2002 23:11 EST
- in response to Dalia Reji
hi
if u don't want a physical document to be created, u should atleast have a template of it.
we tried with a template being on client side which will act as a messenger between layers.
It will be only an xml document with empty tags.When u try to send it it makes attributes for it and adds values to it and sends it to the next layer.
i think it would help.
wrtie to me if u want code sample.
regards,
madhav