简体   繁体   中英

JAXB reusing unmarshalled object

i am new to JAXB.

Currently what I have achieved are:

1) generated classes in eclipse with a .xsd file (predefined by external party)

2) tried marshalling/unmarshalling from sample xml that is complied to the .xsd

What I would like to find out is that:

After unmarshalled, let says the object unmarshalled is "STUDENT", I would like to pass this object to initialize another class which is extended from "STUDENT".

For example (STUDENT),

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "name",
    "age"
})
@XmlRootElement(name = "STUDENT")
public class STUDENT {
//.... sample student class
}

For example (STUDENTEXTENDED),

public class STUDENTEXTENDED extends STUDENT {
//.... sample extended class (I fake this out. May not make sense)
private STUDENT student;
private String homeAddress:
}

if the extended class is called "STUDENTEXTENDED", and I would like to marshall this STUDENTEXTENDED to xml file , how should I declare my root element as there is already a @XmlRootElement in STUDENT class? Can this be done?

Thank you.

You can have multiple root element in JAXB.

Here's an example :

Response.java

@XmlRootElement(name = "response")
public class Response {
...
}

ErrorResponse.java

@XmlRootElement(name = "error")
public class ErrorResponse extends Response {
...
}

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