Archive

Archive for December, 2008

JAXB Undocumented

December 5, 2008 Leave a comment

When marshalling (Java to XML), how to add multiple namespace declaration in xml.

1. Override  NamespacePrefixMapper
2. Override getPreDeclaredNamespaceUris, getPreferredPrefix methods as per your conveninece.

As of today, I did not find the above tip in any document.

Example
import com.sun.xml.bind.marshaller.NamespacePrefixMapper;

public class PrefixMapperImpl extends NamespacePrefixMapper {

@Override
public String[] getPreDeclaredNamespaceUris() {
String[] result = super.getPreDeclaredNamespaceUris();
result = new String[] { “http://www.w3.org/2001/XMLSchema-instance” };
return result;
}

@Override
public String getPreferredPrefix(String namespaceUri, String suggestion,
boolean requirePrefix) {
String prefix = “xsi”;
if (“urn:something.request”
.equals(namespaceUri))
prefix = “prefix”;
return prefix;
}

}

Categories: Uncategorized