2010-12-17 60 views
4

我想在每个我编组的XML文件中定义模式。然后在解组过程中获取这个模式(路径字符串)。 marshaller.setSchema()只有JAXB Marshaller with specified schema

允许调用者验证编组XML时编组。

是的,我可以为这个目的编写额外的豆,但我希望得到的XML像

<root 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation='bla-bla.xsd'>... 

回答

5

要指定noNamespaceSchemaLocation你可以做到以下几点:

JAXBContext jc = JAXBContext.newInstance(Root.class); 
Marshaller marshaller = jc.createMarshaller(); 
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "bla-bla.xsd"); 
+0

这就是我需要的。非常感谢。 – 2010-12-17 18:55:58

+0

解组此属性时出现一些问题。请参阅http://stackoverflow.com/questions/4478666/unmarshaller-and-schema-in-jaxb – 2010-12-20 13:10:42

+0

@Stas Kurilin,我现在已经发布了一个回答,该问题利用XMLStreamReader(StAX解析器)来获取noNamespaceSchemaLocation 。一旦获得noNamespaceSchemaLocation,JAXB就可以使用XMLStreamReader来执行解组。 – 2010-12-20 14:16:32