jeudi 2 février 2017

Convert XML to existing Java Bean using reflection(not JAXB)

The intention of the question is same as in this SO question except that I am trying to achieve without using JAXB. But yeah using reflection which JAXB uses.

I am trying not to use JAXB because I have a predefined bean and when an XML is given, I have to get appropriate classes and setters to fill the bean.

XML is just a data source here, I need to pull whatever data is required for the bean.

I have tried and succeeded for less-complicated XML. But failing for complicated ones like below.

<Response>
    <Result>
        <Result_Flag>2</Result_Flag>
        <Result_Code>1000</Result_Code>
        <Result_Message>Failure</Result_Message>
        <Result_Description>Just for fun2</Result_Description>
    </Result>
    <Remits>
        <OR>
            <I_Number>40002829</I_Number>
            <OrderNumber>agdfsg</OrderNumber>
            <Agents>
                <number>y</number>
                <Agent>
                    <name>a</name>
                    <id>1</id>
                    <phone>
                        <number>9424648525</number>
                        <network>AIRTEL1</network>                              
                    </phone>
                    <phone>
                        <number>9424648525</number>
                        <network>AIRTEL1</network>                              
                    </phone>
                </Agent>
                <Agent>
                    ....similar data...
                </Agent>
            </Agents>
        </OR>
        <OR>
            <I_Number>40004213</I_Number>
            <OrderNumber>fgrtey</OrderNumber>
            <Agents>
                <number>z</number>
                <Agent>
                    <name>c</name>
                    <id>2</id>
                    <phone>
                        <number>9424645555</number>
                        <network>AIRCEL1</network>                              
                    </phone>
                    <phone>
                        <number>9424645555</number>
                        <network>AIRCEL2</network>                              
                    </phone>
                    <I_Number>40002829</I_Number>
                </Agent>
                <Agent>
                    ....similar data...
                </Agent>
            </Agents>
        </OR>
    </Remits>
</Response>

In the above XML Array elements are OR,Agent,phone

Here are the respective beans...

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

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

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

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

I have tried using reflection and failed to implement. Hoping that someone would have tried this...If you know any such implementation please help.

NOTE : Not using JAXB





Aucun commentaire:

Enregistrer un commentaire