简体   繁体   English

用JAXB读取xml

[英]Reading xml with JAXB

I have a XML like that 我有这样的XML

<?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. 我想用Java中的JAXB解析它。 This is my DataSet class. 这是我的DataSet类。

@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. 使用DataSet类创建上下文。

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

Maybe you'll need to add Record as well (not sure): 也许您还需要添加Record (不确定):

@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 DataSet上创建JAXBContext

You need to create your JAXBContext on the DataSet class. 您需要在DataSet类上创建JAXBContext

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

Then since the DataSet class references the Record class, metadata will also be produced for Record . 然后,由于DataSet类引用了Record类,因此还将为Record生成元数据。

If you want to create the JAXBContext on Record 如果要在Record上创建JAXBContext

In your question you created the JAXBContext on Record since record does not reference DataSet no metadata was created for it. 在您的问题中,您创建了Record上的JAXBContext ,因为记录未引用DataSet没有为其创建任何元数据。 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. 如果您仍然希望创建JAXBContextRecord您可以添加一个类型级别@XmlSeeAlso上标注Record在拉DataSet类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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