2
我很難找出簡單的classes的正確模式(以驗證結構和數據類型)。例如,我可以用schemagen
(由JDK提供)獲得Employee
類的答案,但仍然無法使其爲HumanResources
工作。適用於Marshaller的XML模式setSchema
我在嘗試將Employee
類實例的集合序列化爲XML。爲此,我創建了類HumanResources
,其中包含Employee
類元素的列表。例如:
ArrayList<Employee> ems = getTestData();
HumanResources hm = new HumanResources(ems);
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
JAXBContext jaxbContext = JAXBContext.newInstance(HumanResources.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setSchema(sf.newSchema(new File("src\\server\\HumanResources.xsd")));
marshaller.marshal(new JAXBElement<HumanResources>(
new QName(null, "HumanResources"), HumanResources.class, hm), os);