aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to convert a DTO(POJO set with values) to a XML in Java 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 » Engineering » XML and Related Technologies
Bookmark "How to convert a DTO(POJO set with values) to a XML in Java" Watch "How to convert a DTO(POJO set with values) to a XML in Java" New topic
Author

How to convert a DTO(POJO set with values) to a XML in Java

Vivek Hingorani
Ranch Hand

Joined: Jul 08, 2011
Posts: 38
If I have a student DTO with data set into a Student Object and I want to hit a webservices which accepts a XML then how would I convert the DTO to XML?
I have done it though mule transformer but in normal java/xml how it would be done and vice versa??
Vivek Hingorani
Ranch Hand

Joined: Jul 08, 2011
Posts: 38
Can someone from JavaRanch Please reply?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18155
    
    8

I don't know what you mean by "normal Java/XML". However, what you would do first would be to examine the contents of the DTO, and examine the requirements for the content of the XML. Then you would determine the rules for mapping the DTO data items into the XML nodes. Only after all that would you decide how you were going to do the conversion.
Vivek Hingorani
Ranch Hand

Joined: Jul 08, 2011
Posts: 38

The resp getters and setters are a part of class
I want the XML as


How will I achieve this?
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Personally, when I need to do something like this I just write a: either a toXML() method returning a String or a writeXML( Writer out ) method writing to a character stream in the class in question - Student in this case.

Write the opening tag lines, iterate through your collection writing the individual objects and then write a closing tag - Done!

Simpler, and I guarantee faster than any toolkit.

Bill
Remember, XML is just text with a few simple rules.


Java Resources at www.wbrogden.com
Vivek Hingorani
Ranch Hand

Joined: Jul 08, 2011
Posts: 38
Can you please provide me the code if I dont want to use any particular toolkit.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Vivek Hingorani wrote:Can you please provide me the code if I dont want to use any particular toolkit.


No, of course not since I have no idea what your object is like.

If you can manage printing the value of variables to System.out you can certainly figure out the code to write those values inside the required XML tags.

Bill
Vivek Hingorani
Ranch Hand

Joined: Jul 08, 2011
Posts: 38
its a simple student object as i have mentioned.. toXML method might be error prone.. how to do it with marshalling and unmarshalling?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert a DTO(POJO set with values) to a XML in Java
 
Similar Threads
Handling an ajax app
How to update the xml tag attributes for the given tag
Xml Element to Byte Array
Remoting - Communication between swing client and servlet
java program to convert pojo object to pojo xml