How to parse xml using jaxb

I'm new to JAXB, I want to read an XML file for a Java object, which is in the following format:



Please help me how to read this type of XML.


source to share

2 answers

This is your script

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class PayTypeList {

    private List<String> payType;

    public List<String> getPayType() {
        return payType;

    public void setPayType(List<String> payType) {
        this.payType = payType;


Method of use

       public static void main(String[] args) throws JAXBException {
            final JAXBContext context = JAXBContext.newInstance(PayTypeList.class);
            final Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            final PayTypeList paymentType = new PayTypeList();

            List<String> paymentTypes = new ArrayList<String>();

            m.marshal(paymentType, System.out);



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>




For this use case, you will have one class with a property List<String>

. Only the annotations, you may need are @XmlRootElement

and @XmlElement

to map the properties of List

an element payType


Additional Information

You can find more information on using these annotations on my blog:



All Articles