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

The goal of the question is the same as in this SO question , except what I'm trying to achieve without using JAXB. But yes, using reflection that uses JAXB.

I try not to use JAXB because I have a predefined bean, and when XML is given, I have to get the appropriate classes and setters to populate the bean.

XML is just a data source, I need to pull out any data needed for a bean.

I tried and succeeded for less complex XML. But failure for complex ones, sort of 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: OR, Agent, phone

Here are the relevant 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;
}

. , - ... - ​​, , .

. JAXB

+4
1

, xstream, xml- java . .

http://x-stream.imtqy.com/tutorial.html .

.

0

Source: https://habr.com/ru/post/1668694/


All Articles