本文整理汇总了Java中javax.xml.bind.JAXBContext.generateSchema方法的典型用法代码示例。如果您正苦于以下问题:Java JAXBContext.generateSchema方法的具体用法?Java JAXBContext.generateSchema怎么用?Java JAXBContext.generateSchema使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.bind.JAXBContext
的用法示例。
在下文中一共展示了JAXBContext.generateSchema方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: load
import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
public static @NotNull Optional<Schema> load(@NotNull JAXBContext context) {
try {
final List<ByteArrayOutputStream> outputs = new ArrayList<>();
context.generateSchema(new SchemaOutputResolver() {
@Override
public @NotNull Result createOutput(@NotNull String namespace, @NotNull String suggestedFileName) {
final ByteArrayOutputStream output = new ByteArrayOutputStream();
outputs.add(output);
final StreamResult result = new StreamResult(output);
result.setSystemId("");
return result;
}
});
return Optional.ofNullable(
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(outputs.stream()
.map(ByteArrayOutputStream::toByteArray)
.map(ByteArrayInputStream::new)
.map(input -> new StreamSource(input, ""))
.toArray(StreamSource[]::new))
);
} catch (IOException | SAXException e) {
logger.error("Failed to load schema", e);
return Optional.empty();
}
}
示例2: schemagenStackOverflowTest
import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
@Test
public void schemagenStackOverflowTest() throws Exception {
// Create new instance of JAXB context
JAXBContext context = JAXBContext.newInstance(Foo.class);
context.generateSchema(new TestOutputResolver());
// Read schema content from file
String content = Files.lines(resultSchemaFile).collect(Collectors.joining(""));
System.out.println("Generated schema content:" + content);
// Check if schema was generated: check class and list object names
Assert.assertTrue(content.contains("name=\"Foo\""));
Assert.assertTrue(content.contains("name=\"fooObject\""));
}