有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

适用于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) 个答案