Generate xml string from object


#1

Hi,

Is there a way I can generate xml string from an object?
For instance, I got below object.

class Person{
String Name
String Age
String Address
}
class Address{
String AddressLine1
String State
String City
}

Person p1 = new Person()
p1.Name = ‘Mike’
p1.Address = new Address()
p1.Address.City = ‘New York City’

String p1_XML = ??

I want to have p1_XML as below
<Person>
<Name>Mike</Name>
<Age/>
<Address>
<AddressLine1/>
<City>New York City</City>
<State/>
</Address>
</Person>

I know I can use variable and bind from the Request Message. However if the xml gets complicated or has list, it’s very hard to maintain.
How do you guys deal with list and complicated xml request?