aspose file tools*
The moose likes Java in General and the fly likes Generate XML with out JAXB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generate XML with out JAXB" Watch "Generate XML with out JAXB" New topic
Author

Generate XML with out JAXB

sudha swami
Ranch Hand

Joined: Apr 24, 2007
Posts: 179
Hi,
Is there any way to create an XML file from business objects with out using JAXB.
regards
sudha
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by sudha swami:
Is there any way to create an XML file from business objects with out using JAXB.
regards
sudha


I guess this question better fitted for XML forum. ;-)
Anyway , by business objects do you mean value objects ?
Whatever be it ,you can create XML from java objects without using JAXB also.

You can use any templating engine (apache's velocity) for making a robust stuff doing this converstion , or just for some simple xml creations you might just use strings.


Rahul Bhattacharjee
LinkedIn - Blog
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
I frequently give objects a method like:



which is flexible and simplifies testing since the output could go to a file or other character output stream.

Bill


Java Resources at www.wbrogden.com
sudha swami
Ranch Hand

Joined: Apr 24, 2007
Posts: 179
Bill,
I am not clear about the writeXML( PrintWriter out ) you are talking about. Can you please explan with an example.
regards
sudha
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30



[Jess in Action][AskingGoodQuestions]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A writeXLM method would return a chunk of XML for that one object, and perhaps contained objects. For example, if my Person class had only one field for name, it might just say:

That approach bakes the ability to build XML right into every class. That doesn't go well with the Single Responsibility Principle, or with repetitive stress disorder from all the typing.

JAXB builds a generic utility with the ability to generate XML for any nicely formed object. You could make your own with reflection to get the class name, field names, field values:

When you find values that are arrays and collections you'd want to iterate them. When you find values that are other objects, you'd want to turn them into nested XML tags. Pretty soon (only a few thousand hours later) you've rewritten JAXB only with your own flavor of bugs. Whee.

So, why no JAXB? I used to have a sign on the wall that said "JAXB must die" so I might agree, but I want to hear your reasons first.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
sudha swami
Ranch Hand

Joined: Apr 24, 2007
Posts: 179
Hi,
I have the business value addressObj say Address (city,state and zip) and this needs to be copied in the AddressImpl(city,state and zip) generated by JAXB to create an XML.


addressImpl.setCity(addressObj.getCity());
addressImpl.setState(addressObj.getState());
addressImpl.setZip(addressObj.getZip());


This needs to be done for all the business objects. Is there any better way to do it.

I was unable to follow the following method
String toXML(Object obj). If you could give some more details with an example,it would be very helpful.

regards
sudha
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Hi Sudha,

It has already been mentioned as what to do.I will just be repeating the same.

You can have one additional method in you bean class that would return you the chunk of XML that you till now generate using JAXB.

That method can simple return a string have the XML chunk , you would be using the private state variables of you bean class to populate the values within you xml tags.

Hope this helps,

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I have the business value addressObj say Address (city,state and zip) and this needs to be copied in the AddressImpl(city,state and zip) generated by JAXB to create an XML.

If I understand correctly, I've been in exactly the same boat. We used JAXB to generate classes that exactly matched the XML we used with some partner. But those classes did not match the internal business model in our system. So we had to copy all the fields from the JAXB classes into ours to use the model, then copy them all back from the model into JAXB classes to generate XML.

Does that sound right?

On the parsing side, we threw out JAXB. These were huge messages. JAXB sometimes generated nearly 2,000 classes, and it was less effort to write custom parsers than it was to copy every field. We kept JAXB to generate XML because that part was much smaller, only a few classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate XML with out JAXB
 
Similar Threads
generating xml from xsd
JAXB and Illegal Character
Where can I find this class?
add, edit xml record using jsp
how to convert one string to JAXBElement