本文整理匯總了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\""));
}