Assuming you say xsi: type, you mean the “type” attribute from the http://www.w3.org/2001/XMLSchema-instance namespace . This is not what you add to your XML schema, it is a reserved tool for defining an element (similar to a cast in Java).
For correctness:
<myElement myAttribute="whateverstring" xsi:type="hardPart"/>
You will need an XML schema, for example:
<xsd:element name="myElement" type="myElementType"/>
<xsd:complexType name="myElementType">
<xsd:attribute name="myAttribute" type="xsd:boolean" />
</xsd:complexType>
<xsd:complexType name="hardPart">
<xsd:complexContent>
<xsd:extension base="myElementType">
...
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
, XML , "hardPart", :
<myElement myAttribute="whateverstring" xsi:type="hardPart"/>
myElement - "myElementType" xsi: type = "hardPart", , "hardpart".
JAXB
MyElementType
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MyElementType {
private String myAttribute;
@XmlAttribute
public void setMyAttribute(String myAttribute) {
this.myAttribute = myAttribute;
}
public String getMyAttribute() {
return myAttribute;
}
}
HardPart
public class HardPart extends MyElementType {
}
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(HardPart.class, MyElementType.class);
HardPart hardPart = new HardPart();
hardPart.setMyAttribute("whateverstring");
JAXBElement<MyElementType> jaxbElement = new JAXBElement(new QName("myElement"), MyElementType.class, hardPart);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(jaxbElement, System.out);
}
}