Fastest way to build up a XML message.

Discussions

XML & Web services: Fastest way to build up a XML message.

  1. Fastest way to build up a XML message. (9 messages)

    Hi guys,

    We're doing some XML-RPC pushing between the server and our clients and we recently found that our way of building the XML message is a bottleneck in our process.
    We do it the basic way: stringBuffer.append("<xml>").append("<client>").append("<name>").append(userEntityBean.getUsername())...
    The thing is really going slow and we're already catching all the non-user related data.
    I was suggested to use a DOM, replace the user specific data and output the message but AFAIK any toXML() method is also based on stringBuffer(). Is there any faster option?
    We're using Weblogic 7.00.1 if it's reelevant.

    Thanks!

    Gerard.

    Threaded Messages (9)

  2. Fastest way to build up a XML message.[ Go to top ]

    Gerard,

    How much of your document is the same every time? If alot of it is identical every time, you might find it faster to build a template with tokens for the data to be changed, and then only replace the tokens.

    Dave Wolf
    Personified Technologies LLC
  3. Part of the problem may be the remote calls to the ejbs, i.e. entitybean.getName(). I think that you need to get the data in the object you are using to build the xml.
  4. Fastest way to build up a XML message.[ Go to top ]

    Hi

    My current project is purely XML Messaging based and we have used a template class to generate the XML. The class returns whole XML String by using obj.getExternalMessage()

    The template class use series of getter/setter methods which is used to set member variables from the calling class. Hence generation of XML is concentrated at one place and biggest advantage is that incase your schema changes, you need to modify at one place. Hence clean code and high maintainability.

    We are using websphere and the response time (the time when request is sent to server and response generated by server) is too good < 3 sec per request. Believe me our XML is very complex and handles 30 entities, and performance is good.

    Hope this will solve your problem.

    Somal
  5. Template Class.[ Go to top ]

    Hi,

    Thanks for the tip. The template class you're referring to is a simple class with set's and a function to produce the xml? A kind of class with a toString() in XML ?
    I agree with Gareth in that time is spent going to the entities but that is obviously unavoidable.
    The token idea sounds good but again, how to I assamble the message? I'll end up with StringBuffer() as I do now and at the end I'll have no win.
    The template suggestion will help me keep things maintainable as the stringbuffer.append().append().append().append() is becoming a hell.

    Somal, I'd like to know the details of your template method, if possible.

    Thanks to all!

    Gerard.
  6. Template Class.[ Go to top ]

    Of course you need to get the information from the entity beans, but you should do them with a single remote call returning a Value Object / Data Transfer Object containing all the finer grained values. That improves performance as the network call and marshalling is made only once.

    Also, you can improve the performance of using StringBuffer if you combine your append().append().append()... into as few final static String's as possible. Also, remember to set the initial size of the StringBuffer into something convenient when instantiating it: new StringBuffer(1024);

    I mean, instead of

    buffer.append("<root>").append("<sub>");
    buffer.append("<value>").append(someValue).append("</value>");
    buffer.append("</sub>").append("</root>");

    Do something like this

    buffer.append("<root><sub><value>");
    buffer.append(someValue);
    buffer.append("</value></sub></root>");

    and replace "abc" with final static String ABC
  7. Small update.[ Go to top ]

    Lasse,

    I did some tests regarding the use of inline declared strings ("ABC") vs. static final String ABC = "ABC"
    when appended to a string or stringbuffer.
    In both cases using the inline String was added 5x faster than the static final version.

    Just for your info.

    -regards,

    Gerard.
  8. Template Class.[ Go to top ]

    Hi Gerard

    We also tried to use same methology which you were using. But it doesn't work well on performance issue as one of the client requirement was Max. 3 sec response time (infact we never bother about multiple 'append' expressions).
    We use castor to generate the classes. It has very efficient way to marshal and unmarshal the XML. To made it more efficient we write some wrappers to it and it all works.
    I would suggest you to use castor in your case and you will be save by multiple 'append()' expressions.

    Regarding wrappers, these are the templates which are the navigations, locators and counters and perform the respective functions for different entities.(Example: A person can have multiple address. Hence navigator will traverse all address one by one. Locators are used to locate particular address. Counters give you total number of address person has.)

    Hope you must have got an idea how to implement it.

    Somal
  9. may be have a look @ JDom www.jdom.org its been designed to simplfy the build of xml docs
  10. take a look at cocoon generators