2
단순한 classes의 올바른 스키마 (구조 및 데이터 유형의 유효성을 검사하기)를 파악하는 데 어려움을 겪고 있습니다. 예를 들어, Employee
클래스의 응답을 schemagen
(JDK와 함께 제공)으로 얻을 수 있지만 여전히 HumanResources
에서 작동하지 않습니다.Marshaller에 적합한 XML 스키마 setSchema
XML에 Employee
클래스 인스턴스의 컬렉션을 직렬화하려고합니다. 이를 위해 Employee
클래스 요소 목록을 포함하는 클래스 HumanResources
을 만들었습니다. 예 :
먼저 당신이 javax.xml.bind.SchemaOutputResolver를 확장하는 클래스를 작성해야합니다 : 아래
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);