适用于Marshaller setSchema的java XML模式
我很难找出简单classes的正确模式(以验证结构和数据类型)。例如,我可以用schemagen
(随JDK提供)获得Employee
类的答案,但仍然无法让它为HumanResources
工作
我正在尝试将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);
共 (0) 个答案