具有指定模式的JAXB Marshaller

Sta*_*lin 4 java xsd jaxb

我想在我编组的每个xml文件中定义模式.然后在解组过程中获取此模式(路径字符串).marshaller.setSchema()只要

允许调用者在编组时验证编组的XML.

是的,我可以为此目的编写额外的bean,但我希望得到xml之类的

<root
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation='bla-bla.xsd'>...
Run Code Online (Sandbox Code Playgroud)

bdo*_*han 5

要指定noNamespaceSchemaLocation,您可以执行以下操作:

JAXBContext jc = JAXBContext.newInstance(Root.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "bla-bla.xsd");
Run Code Online (Sandbox Code Playgroud)