I'd suggest the following approach. Define the schema in the usual way,
giving names to your types:
<xsd:schema xmlns:xsd="
http://www.w3.org/2001/XMLSchema"
xmlns:jxb="
http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0">
<xsd:complexType name="SensorBasicReport">
<xsd:sequence>
<xsd:element name="messageType" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="AnyReport">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="content" type="xsd:anyType"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
To marshal, create the content tree. Finally, wrap the top-level element
into a JAXBElement object and pass this to the marshaller. (The first
argument of my marshal method is what is passed to Marshaller's marshal.)
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import util.XmlMarshaller;
import generated.AnyReport;
import generated.SensorBasicReport;
import generated.ObjectFactory;
public class TestReport {
public static void main( String[] args ) throws Exception {
ObjectFactory of = new ObjectFactory();
SensorBasicReport sbr = of.createSensorBasicReport();
sbr.setMessageType( "message type" );
AnyReport ar = of.createAnyReport();
ar.setTitle( "AnyReport Title" );
ar.setContent( sbr );
JAXBElement<AnyReport> jbe =
new JAXBElement<AnyReport>( new QName( "anyReport" ) ,
AnyReport.class,
ar );
XmlMarshaller m = new XmlMarshaller();
m.marshal( jbe, "rootelem.xml" );
}
}
This will give you:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<anyReport>
<title>AnyReport Title</title>
<content xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:type="SensorBasicReport">
<messageType>message type</messageType>
</content>
</anyReport>
It should be possible to marshal a SensorBasicReport in the same way.
Regards
Wolfgang