2010-12-17 59 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