This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Best way to manipulate XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Best way to manipulate XML" Watch "Best way to manipulate XML" New topic

Best way to manipulate XML

Jo Goddard

Joined: Oct 02, 2003
Posts: 1
Dear All
I'm fairly new to this XML business so please excuse any ignorance I may display!
I am currently trying to come up with good way forward for building some XML. We have to produce a fairly complicated XML structure - approximately 80 tags amny with attributes - OK so maybe that is fairly simple by some standards! The majority of the information within the XML is going to be fixed and we just need to add in values for about 10 to 15 tags.
The plan is to read in a file of XML data with all the tags and fixed data already in place. We then just need to populate those few variable fields.
My question is what might be the "best" way to do this population.
We would prefer not to use DOM as the object model produced will be large and mostly unneccessary.
We have consider using Stringbuffer to simple append this data in the right places - we feel that this is not very developer friendly and unwieldly.
Am currently lookng at using the MessageFormat class whereby you can poulate your XML by adding {1}, {2}, ...markers in the default XML and then replace these markers with data from an array of objects - it's easier to use than Stringbuffer but only allows up to 10 markers. I am thinking of creating my own version of MessageFormat and removing the limitation of 10 markers and perhaps personalising the markers so that they have names rather than numbers.
Anybody else been here and found a more useful part of the API or got a better idea?
Balaji Loganathan
author and deputy

Joined: Jul 13, 2001
Posts: 3150
How about castor ?
Just give a try, don't spend too much time.

Spritle Software Blogs
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
If speed is really critical, treating the XML as text and patching with the require varying values will be a lot faster than building a DOM or other representation of the whole document. There are various template utilities that people use for this sort of thing.
To suggest more optimization we would need to know more about the application and the user load it must support.
Jo Fail

Joined: Feb 05, 2003
Posts: 18
To be honest this application will be fairly low usage and I can't see the overheard of building a DOM being a problem although under the circumstances I do think that it is overkill. I guess that an ideal would be a class or two that given an XML string with markers can take collection class of name/value pairs and replace the markers
For example, given an XML string thus
and a collection containing (forename,Jo),(init,C),(surname,Goddard)
would result in
Yes we could write it fairly easily but a previously tried and tested API would be nice!!! I would have thought that this problem has been encountered before - maybe they just used DOM. Maybe we should just use DOM ....
I agree. Here's the link:
subject: Best way to manipulate XML
It's not a secret anymore!