简体   繁体   中英

Parent child JAXB Unmarshalling - Java

<data>
    <payment>
        <paymentitems>
            <paymentitem>
            </paymentitem>
            <paymentitem>
            </paymentitem>
        </paymentitems>
        <paymentitemdetails>
            <paymentitemdetail>
            </paymentitemdetail>
            <paymentitemdetail>
            </paymentitemdetail>
        </paymentitemdetails>
        <lineitems>
            <lineitem>
            </lineitem>
            <lineitem>
            </lineitem>
        </lineitems>
    </payment>
</data>

I want to unmarshal the above mentioned xml string. I am confused how can I start with this as this is nested and having parent child relationships.I have done the unmarshalling for Single element xml which means if it is ... I am struggling to find a start here.

Singular Relationships

There isn't anything special you need to do to map relationships.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Data {

    private Payment payment;

}

Collection Fields/Properties

You will want to look at the @XmlElementWrapper annotation for mapping your collections since they have a grouping element:

 @XmlAccessorType(XmlAccessType.FIELD)
 public class Payment {

    @XmlElementWrapper(name="paymentitems")
    @XmlElement(name="paymentitem")
    private List<PaymentItem> paymentItems;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM