File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes Bean is populated with null values while conversion of xml to bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Bean is populated with null values while conversion of xml to bean" Watch "Bean is populated with null values while conversion of xml to bean" New topic
Author

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

palakolanusrinu Reddy
Greenhorn

Joined: Jan 03, 2008
Posts: 24
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean is populated with null values while conversion of xml to bean