2011-07-28 81 views
2

当我有下不同的命名空间10点的模式,每一个的complexType和相关联的元件,其为所谓的“报告”的元素的取代,例如不工作<xsd:element name="Report" abstract="true"/>JAXB继承解组

当我解组已经在所有10份报告的XML文档,只有5当我做了以下发现:

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); 
    ... 
} 

这与xml中的报告顺序或报告数量无关。 10种类型报告中的5种始终可以找到。我已经使用JAXB的参考实现2.24u1和2.1.10(JDK 6版本),都无济于事。看起来替换组被忽略。任何帮助将非常感激!

+1

报告中只有特定类型的子类没有解组吗?是com.foo.bar jaxb.in​​dex或ObjectFactory中Report的所有子类吗? – padis

+0

只有某些类型的“报告”未被解组。每种报告类型都在其自己的名称空间中。例如, 'com.foo.bar.report1.Myreport1' 'com.foo.bar.report2.Myreport2' – michael

+0

Report的子类的相同类型(5/10)不会每次解组。 Report的每个子类都有其自己的名称空间,模式和ObjectFactory类。 “ReportPackage”模式将所有这些不同的报告结合在一起,每个报告都可以替代抽象的“报告”元素。 ReportPackage有一个ObjectFactory类,它包含一个'createReport(Object obj)'方法。 – michael

回答

1

尝试使用这种方法创建JAXB上下文:JAXBContext.newInstance("package1:package2:package3:...")其中packageX是您的ObjectFactory所在的包。

+0

谢谢padis。此解决方案工作。我已经尝试过,但我一直在使用“;”作为我的包装分隔符。 – michael