Is it possible to customize the namespace prefix that JAXB uses when marshalling to a String?

前端 未结 4 519
暖寄归人
暖寄归人 2020-11-30 06:45

For example, I\'ve got a simple schema which imports another schema. The second schema (urn:just:attributes, just-attributes.xsd) just defines an attribute group.



        
4条回答
  •  攒了一身酷
    2020-11-30 07:05

    http://hwellmann.blogspot.com/2011/03/jaxb-marshalling-with-custom-namespace.html

    This shows how to do it.

    Another: http://www.systemmobile.com/?p=280

    Key bits in case that link dies too:

    the NamespacePrefixMapper class, found in the com.sun.xml.bind.marshaller package. The abstract class has one method to implement:

    public abstract String getPreferredPrefix(  
         String namespaceUri,         
         String suggestion,         
         boolean requirePrefix); 
    

    then

    Marshaller marshaller =        
        jaxbContext.createMarshaller();        
    marshaller.setProperty(”com.sun.xml.bind.namespacePrefixMapper”,        
        new MyNamespacePrefixMapper());  
    

    If you’re also using javax.xml.xpath.XPath, your NamespacePrefixMapper can also implement javax.xml.namespace.NamespaceContext, centralizing your namespace customization in a single class.

提交回复
热议问题