• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Bean is populated with null values while conversion of xml to bean

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please help any one in this while converting xml to bean, here I am using betwixt.This xml contains normal data as well as collections data but normal data and some of collection data is populating in beans and some collection data is populating beans with null values.

XML response is :
<?xml version="1.0" encoding="UTF-8"?>
<Hostel noOfHostels="1">
<HostelRoom
roomNo="0004105232" roomLocation="2101086158" roomPlace="2622" bookedOn="08/27/2008 03:07:56 PM"
roomName="TAMIL NADU" quota="GN" class="SL" source="ndls" destination="mas"
day="28" month="08" year="2008" bordingdate="08/28/2008 12:00:00 AM"
internetServiceCharge="20.00" roomFare="2696.00" totalFare="2716.00" idPassenger="1">

<Passenger slNo="1" name="TEST" age="35" sex="m" concessionCode="BLANK" bookingStatus="RLWL 6" />
<Passenger slNo="2" name="aaaa" age="85" sex="m" concessionCode="NOCONC" bookingStatus="RLWL 7" />

</HostelRoom>
</Hostel>

and betwixt mapping is
MAPPING = new String(
"<?xml version=\"1.0\"?>"
+ "<betwixt-config>"
+ "<!--name of the class to map -->"
+ "<class com.booking.BookingSearchResponse\">"
+ "<element name=\"Hostel\">"
+ "<attribute name=\"noOfHostels\" property=\"noOfHostels\"/>"
//+ "<element name=\"HostelRoom\">"
+ "<element name=\"HostelRoom\" property=\"hostelRoom\" updater=\"addHostelRoom\" class=\"com.booking.BookedTicket\"/>"
+ "</element>"
+ "</class>"
+ "<class name=\"com.booking.BookedTicket\">"
+ "<element name=\"HostelRoom\">"
+ "<attribute name=\"roomNo\" property=\"roomNo\"/>"
+ "<attribute name=\"roomLocation\" property=\"roomLocation\"/>"
+ "<attribute name=\"quota\" property=\"quota\"/>"
-----
------
+ "</element>"
+ "<element name=\"Passenger\">"
+ "<element name=\"Passenger\" property=\"passenger\" updater=\"addPassenger\" class=\"com.booking.BookedPassenger\"/>"
+ "</element>"
+ "</class>"
+ "<class name=\"com.booking.BookedPassenger\">"
+ "<element name=\"Passenger\">"
+ "<attribute name=\"slNo\" property=\"slNo\"/>"
+ "<attribute name=\"name\" property=\"name\"/>"
+ "<attribute name=\"age\" property=\"age\"/>"
+ "<attribute name=\"sex\" property=\"sex\"/>"
+ "<attribute name=\"concessioncode\" property=\"concessionCode\"/>"
+ "<attribute name=\"bookingstatus\" property=\"bookingStatus\"/>"

+ "</element>" + "</class>"
+ "</betwixt-config>");

and i created 3 POJOs

1)BookingSearchResponse.java
2)BookedTicket.java
3)BookedPassenger.java

BookingSearchResponse.java
//included updater
public void addHostelRoom(BookedTicket bookedTicket) {
(this is arrayList)hostelRoom.add(bookedTicket);
}

BookedTicket.java
public void addPassenger(BookedPassenger passenger){
(this is arrayList)passenger.add(passenger);


}
Here noOfHostels and passenger collection is populating properly but HostelRoom collection is not populating please help in this

to parse this we used

public static Object xmlToBean(Object object, String xml)throws Exception{
BeanReader beanReader = null ;
try {
beanReader = new BeanReader();
//Set the ConvertUtils Object for String Conversion of the Default primitive classes
beanReader.getBindingConfiguration()
.setObjectStringConverter(new ObjectStringConverter());

beanReader.getBindingConfiguration().setValueSuppressionStrategy(new AttributeValueSuppressionStrategy());
String temp = (String)object.getClass().getField("MAPPING").get(object);
beanReader.registerMultiMapping(new InputSource(new StringReader(temp)));

Object parsedObject = beanReader.parse(new StringReader(xml));

return parsedObject;

}

Regards,
Srinivasa Reddy.P
 
Villains always have antidotes. They're funny that way. Here's an antidote disguised as a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic