I have 10 schemas under different namespaces, each with a complexType and associated element that is a substitution for an element called ‘Report’, for example:
<xsd:element name="MyReport" type="foo:MyReportType" substitutionGroup="bar:Report"/>
<xsd:element name="Report" abstract="true"/>
When I unmarshal an XML document that has all 10 reports in it, only 5 are found when I do the following:
JAXBContext context = JAXBContext.newInstance("com.foo.bar");
Unmarshaller unmarshaller = context.createUnmarshaller();
//get root node of xml
JAXBElement<ReportPackage> package = unmarshaller.unmarshal(new StreamSource(new File("example.xml")));
ReportPackage rp = package.getValue();
ReportSection reports = rp.getReports();
List<JAXBElement<?>> reportList = reports.getReport();
//iterate through the 10 reports
for (int i = 0; i < reportList.size(); i++) {
JAXBElement rep = reportList.get(i);
...
}
It does not matter the order of the reports or number of reports within the xml. The same 5 of the 10 types of reports are consistently found. I’ve used the reference implementation 2.24u1 and 2.1.10 (JDK 6 version) of JAXB, both to no avail. It appears that the substitutionGroup is ignored. Any help would be much appreciated!
Try to create JAXB context this way:
JAXBContext.newInstance("package1:package2:package3:...")wherepackageXs are packages where your ObjectFactories are.