Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Answer to this question starts from basic java principal. Since derived class override setName and getName . (new Employee()).setName("John") populates Employee's name not Person's. So if you want to populate properties of both the classes you should change person class to </p> <pre><code>@XmlSeeAlso(Employee.class) public abstract class Person { protected String firstName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } } </code></pre> <p>and do marshal of this.</p> <pre><code>Employee e = new Employee(); e.setName("John"); e.setLastName("JohnLast"); </code></pre> <p>then your xml will look like</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;employee&gt; &lt;lastName&gt;JohnLast&lt;/lastName&gt; &lt;name&gt;John&lt;/name&gt; &lt;/employee&gt; </code></pre> <p>and xsd </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:element name="employee" type="employee"/&gt; &lt;xs:complexType name="employee"&gt; &lt;xs:complexContent&gt; &lt;xs:extension base="person"&gt; &lt;xs:sequence&gt; &lt;xs:element name="name" type="xs:string" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:extension&gt; &lt;/xs:complexContent&gt; &lt;/xs:complexType&gt; &lt;xs:complexType name="person" abstract="true"&gt; &lt;xs:sequence&gt; &lt;xs:element name="lastName" type="xs:string" minOccurs="0"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>and during unmarshalling following works and name and lastName gets populated,</p> <pre><code>JAXBContext context = JAXBContext.newInstance(Employee.class); Unmarshaller unmarshaller = context.createUnmarshaller(); Employee e = (Employee) unmarshaller.unmarshal(xml); Person p = (Person) unmarshaller.unmarshal(xml); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload