使用反射(而不是JAXB)将XML转换为现有Java Bean

时间:2022-02-28 21:49:58

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.

问题的意图与此SO问题相同,只是我试图在不使用JAXB的情况下实现。但是使用JAXB使用的反射。

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.

我试图不使用JAXB,因为我有一个预定义的bean,当给出XML时,我必须得到适当的类和setter来填充bean。

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

XML只是一个数据源,我需要提取bean所需的任何数据。

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

我已经尝试并成功完成了不那么复杂的XML。但是没有像下面那样复杂的那些。

<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

在上面的XML数组元素是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

注意:不使用JAXB

1 个解决方案

#1


0  

I think you can use xstream, this is a library to convert xml data to java class and other way. is very simple to use.

我认为你可以使用xstream,这是一个将xml数据转换为java类和其他方式的库。使用起来非常简单。

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

http://x-stream.github.io/tutorial.html这是一个教程。

I have used for more complex structure and works fine.

我用于更复杂的结构,工作正常。

#1


0  

I think you can use xstream, this is a library to convert xml data to java class and other way. is very simple to use.

我认为你可以使用xstream,这是一个将xml数据转换为java类和其他方式的库。使用起来非常简单。

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

http://x-stream.github.io/tutorial.html这是一个教程。

I have used for more complex structure and works fine.

我用于更复杂的结构,工作正常。