1

i have a xml file with this content. each transaction starts with trans_ and ends with transaction number

<trans_1>
    <date>2013/10/10</date>
    <code>109129</code>
    <amount>+000000024000000</amount>
    <balance>+000000024000000</balance>
</trans_1>
<trans_2>
    <date>2013/10/10</date>
    <code>109149</code>
    <amount>+000000025000000</amount>
    <balance>+000000049000000</balance>
</trans_2>
<trans_3>
    <date>2013/10/10</date>
    <code>109161</code>
    <amount>+000000005000000</amount>
    <balance>+000000054000000</balance>
</trans_3>

i want to map this array to list of following object.

class Transaction{
    string date;
    string code;
    string amount;
    string balance;


   /**** getter and setter ***/
}
Soheil Ghahremani
  • 1,135
  • 1
  • 16
  • 36
  • JAXB makes this sort of thing very easy. Check out Blaise's answer to http://stackoverflow.com/questions/11221136/convert-xml-to-java-object-using-jaxb-unmarshal for a very detailed example. – Stephen Carlson Sep 27 '14 at 08:15
  • @StephenCarlson tnx. but the tag names aren't identical. and ... . i can't use @XmlElement(name="trans") for example – Soheil Ghahremani Sep 27 '14 at 08:27
  • 1
    Do you have any control over the schema? It would be much better XML form to transmit the id as an attribute instead of embedding it in the tag name. If not, you could use @XmlAnyElement, but you'd end up with DOM objects that you'd have to do some manual mapping from to achieve your desired result. – Stephen Carlson Sep 27 '14 at 08:41
  • @StephenCarlson no i don't have control over schema. it is response from a web service – Soheil Ghahremani Sep 27 '14 at 10:44

1 Answers1

0

i found a solution with @XmlElements

@XmlAccessorType(XmlAccessType.FIELD)
public class DepositLast3CycleResponse extends BaseResponseBean {

    @XmlElements(
            {
                    @XmlElement(name = "trans_1"),
                    @XmlElement(name = "trans_2"),
                    @XmlElement(name = "trans_3")
            }
    )
    private List<TransactionInfo> transactions;


    /****** getters and setters ****/
}
Soheil Ghahremani
  • 1,135
  • 1
  • 16
  • 36