简体   繁体   中英

Reading xml with JAXB

I have a XML like that

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set>
    <record>
        <TARIH>data</TARIH>
        <GUNLER>data</GUNLER>
        <YEMEK1>data</YEMEK1>
        <YEMEK2>data</YEMEK2>       
    </record>
    <record>
        <TARIH>data</TARIH>
        <GUNLER>data</GUNLER>
        <YEMEK1>data</YEMEK1>
        <YEMEK2>data</YEMEK2>   
    </record>
</data-set>

And I want to parse it with JAXB in Java. This is my DataSet class.

@XmlRootElement(name="data-set")
@XmlAccessorType(XmlAccessType.FIELD)
public class DataSet {
    @XmlElement(name="record")
    private List<Record> records = null;
    public List<Record> getRecords(){
        return records;
    }

    public void setRecords(List<Record> records){
        this.records = records;
    }
}

And this is my Record class.

@XmlRootElement(name="record")
@XmlAccessorType(XmlAccessType.FIELD)
public class Record {
    String TARIH,GUNLER,YEMEK1,ANAYEMEK1,ANAYEMEK2,YEMEK3,YEMEK4,SALATBAR1,SALATBAR2,SALATBAR3,SALATBAR4,SALATBAR5;

    //getters and setters//

I try something like that.

public class Main {
    public static void main(String[] args) throws JAXBException {
        File file = new File("C:/Users/EMRE/Desktop/YEMEKHANE DATABASE/morning.xml");
        JAXBContext jaxbcontext = JAXBContext.newInstance(Record.class);
        Unmarshaller jaxbunmarshaller = jaxbcontext.createUnmarshaller();
        Record record = (Record)jaxbunmarshaller.unmarshal(file);

        System.out.println(record.getTARIH());
    }
}   

And I faced error like this.

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"data-set"). Expected elements are <{}record>

How can I fix this? Thank you.

Create your context with DataSet class.

JAXBContext jaxbcontext = JAXBContext.newInstance(DataSet.class);

Maybe you'll need to add Record as well (not sure):

@XmlSeeAlso({Record.class})
public class DataSet {...}

But I think it may work even without it.

Alternatively you could do:

JAXBContext jaxbcontext = JAXBContext.newInstance(DataSet.class, Record.class);

There are further alternatives with package name-based context path as well. Just not so straightforward if you write your classes manually.

Create the JAXBContext on DataSet

You need to create your JAXBContext on the DataSet class.

    JAXBContext jaxbcontext = JAXBContext.newInstance(DataSet.class);

Then since the DataSet class references the Record class, metadata will also be produced for Record .

If you want to create the JAXBContext on Record

In your question you created the JAXBContext on Record since record does not reference DataSet no metadata was created for it. If you still want to create the JAXBContext on Record you can add a type level @XmlSeeAlso annotation on Record to pull in the DataSet class.

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