2
シンプルなclassesの正しいスキーマ(構造とデータ型を検証するため)を見つけるのは苦労しています。たとえば、Employee
クラスの回答をschemagen
(JDKに付属)で得ることができましたが、それでもHumanResources
で動作するようにはできませんでした。Marshallerの適切なXMLスキーマsetSchema
私はEmployee
クラスインスタンスのコレクションをXMLにシリアル化しようとしています。そのために、クラスのクラス要素のリストを含むクラスHumanResources
を作成しました。例:以下は
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);