Use reflection (not JAXB) to convert XML to existing Java Bean

The intent of this question is the same as in this SO question, except that I tried to implement it without JAXB. But use the reflection used by JAXB.

< p>I tried not to use JAXB because I have a predefined bean, and when XML is given, I have to get the appropriate class and setter to fill the bean.

XML is just a data source, I need Extract any data required by the bean.

I have tried and successfully completed the less complex XML. But nothing is as complex as below.

 

2
1000
Failure
Just for fun2



40002829
agdfsg

y

a
1

9424648525
AIRTEL1


9424648525
AIRTEL1



....similar data...


< br />
40004213
fgrtey

z< br />
c
2

9424645555< br /> AIRCEL1


94246455 55
AIRCEL2

40002829

< Agent>
....similar data...




The above XML array element is OR,Agent,phone

This is the corresponding bean…

public class SampleBean {
private String responseCode;
private String responseMessage;
private ArrayList records;
private String txnId;
}

public class OR{
private String txnId;
private String orderNumber;
private String numberOfAgents;
private ArrayList otherAgents;
}< br />
public class Agent {
private String agentName;
private String agentId;
private ArrayList agentPhoneDetail s;
}

public class Phone {
private String agentPhoneNumber;
private String agentPhoneNetwork;
}

I tried Use reflection and cannot be achieved. Hope someone has tried this… If you know of any such implementation, please help.

Note: Do not use JAXB

I think you can use xstream, which is a library that converts xml data into java classes and other ways. It’s very simple to use.

http://x-stream.github.io/tutorial.html here is a tutorial.

I used it for a more complex structure and it works fine.

< /p>

The intent of the question is the same as in this SO question, except that I tried to achieve it without using JAXB. But using the reflection used by JAXB.

I tried not Using JAXB, because I have a predefined bean, when given XML, I have to get the appropriate class and setter to fill the bean.

XML is just a data source, I need to extract what the bean needs Any data.

I have tried and successfully completed the less complex XML. But nothing is as complex as the following.



2
1000
Failure
Just for fun2



40002829
agdfsg

y

a
1

9424648525
AIRTEL1


9424648525
AIRTEL1



....similar data...




< I_Number>40004213
fgrtey

z
c
2

9424645555< br /> AIRCEL1


9424645555
AIRCEL2

40002829


....similar data...




The XML array above The element is OR,Agent,phone

This is the corresponding bean…

public class SampleBean {
private String responseCode;
private String responseMessage;
private ArrayList records;
private String txnId;
}

public class OR{
private String txnId;
private String orderNumber;
private String numberOfAgents ;
private ArrayList otherAgents;
}

public class Agent {
private String agentName;
private String agentId;
private ArrayList agentPhoneDetails;
}

public class Phone {
private String agentPhoneNumber;
private String agentPhoneNetwork;
}

I have tried using reflection and can’t achieve it. Hope someone has tried this… If you know of any such implementation, please help.

Note: Do not use JAXB

I think you can use xstream, which is a library that converts xml data into java classes and other ways. It is very simple to use.

http://x -stream.github.io/tutorial.htmlThere is a tutorial here.

I used it for more complex structures and it works fine.

Leave a Comment

Your email address will not be published.