*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Best way to manipulate XML" Watch "Best way to manipulate XML" New topic
Author

Best way to manipulate XML

Jo Goddard
Greenhorn

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
Bartender

Joined: Jul 13, 2001
Posts: 3150
How about castor http://castor.exolab.org/xml-framework.html ?
Just give a try, don't spend too much time.


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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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.
Bill
Jo Fail
Greenhorn

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
<name>
<forename>{forename}</forename>
<middle_intial>{init}</middle_initial>
<surname>{surname}</surname>
</name>
and a collection containing (forename,Jo),(init,C),(surname,Goddard)
would result in
<name>
<forename>Jo</forename>
<middle_initial>C<middle_initial>
<surname>Goddard</surname>
</name>
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 ....
 
jQuery in Action, 2nd edition
 
subject: Best way to manipulate XML
 
Similar Threads
Java objects to XML, possibly using JAXB
Best Way to present XML in GUI app?
Viewing XML
Using XSL Transform on Java Beans with Lazy Loading?
Help with design of stock class when the stock's web data is part of a group.