简体   繁体   English

JAXB Marshall & Unmarshall Java 复杂类

[英]JAXB Marshall & Unmarshall Java complex Class

I'm working with JAXB in a Java project.我正在 Java 项目中使用 JAXB。

I'd like to process my data with JAXB but i have a problem with these classes.我想用 JAXB 处理我的数据,但我对这些类有问题。

What kind of tags and so on should I add to the fields to get marshalling and unmarshalling working?我应该在字段中添加什么样的标签等才能使编组和解组工作?

Wrapper.class包装类

@XmlElement(name="database")
public class Wrapper {

    private List <Person> persons;
    private List <Quote> quotes;

    @XmlElement(name="persons")
    public List<Person> getPersons(){
        return=this.persons;
    }

    @XmlElement(name="quotes")
    public List<Quote> getQuotes(){
        return=this.quotes;
    }    
}

Person.class人物类

@XmlRootElement(name="person")
public class Person {

    private final StringProperty name;
    private final StringProperty telephone;
    private final StringProperty mail;
    private final ObjectProperty<LocalDate> date;
    //I have problem with this complex field:
    private final ObjectProperty<List <Job>> jobs;

    //Constructor getters and setters
    ...
}

Job.class作业类

@XmlRootElement(name="job")
public class Job {

    private final StringProperty roleName;
    private final StringProperty id;
    //I have problem with this complex field too:
    private final ObjectProperty<List <Subrole>> subroles;


    //Constructor getters and setters
    ...
}

Subrole.class子角色类

@XmlRootElement(name="subroles")
public class SubRole {
    private final StringProperty subRoleName;
    private final ObjectProperty<List <String>> actions;

    //really don't know how to treat this
    private final ObjectProperty<List<List <Activity>>> activities;

    //Constructor getters and setters
    ...
}

Activity.class活动类

@XmlRootElement(name="activities")
public class Activity{
    private final StringProperty activityName;
    private final FloatProperty salary;
    private final FloatProperty hours;


    //Constructor getters and setters
    ...
}

If you have lack of knowledge about that topic and if you are using Eclipse, I can advice you creating your own XSD(XML Schema Definiton) file according to your class hierarchy (You can try Altova XML Spy eg).如果您对该主题缺乏了解,并且您正在使用 Eclipse,我可以建议您根据您的类层次结构创建您自己的 XSD(XML 架构定义)文件(例如,您可以尝试 Altova XML Spy)。 After that, move that xsd to your project and generate JAXB classes in Eclipse automatically(Right click to xsd and you will see generate option).之后,将该 xsd 移动到您的项目并在 Eclipse 中自动生成 JAXB 类(右键单击 xsd,您将看到生成选项)。

It will generate your JAXB classes, thus you can inspect them and learn how to manage those type of objects.它将生成您的 JAXB 类,因此您可以检查它们并学习如何管理这些类型的对象。

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

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