• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vivek Hingorani
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone from JavaRanch Please reply?
 
Paul Clapham
Sheriff
Pie
Posts: 20978
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Vivek Hingorani
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its a simple student object as i have mentioned.. toXML method might be error prone.. how to do it with marshalling and unmarshalling?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic