Java Utililty Methods XML Namespace

List of utility methods to do XML Namespace

Description

The list of methods to do XML Namespace are organized into topic(s).

Method

StringgetNamespace(Class klass)
get Namespace
String namespace = null;
XmlType xmlType = klass.getAnnotation(XmlType.class);
if (xmlType != null) {
    String ns = xmlType.namespace();
    if (!DEFAULT_NAMESPACE_VALUE.equals(ns)) {
        namespace = ns;
if (namespace == null) {
    namespace = DEFAULT_NAMESPACE_VALUE;
    Package pkg = klass.getPackage();
    XmlSchema xmlSchema = pkg.getAnnotation(XmlSchema.class);
    if (xmlSchema != null) {
        namespace = xmlSchema.namespace();
return namespace;
StringgetNamespace(final Package targetPackage)
get Namespace
String namespaceURI;
if (targetPackage == null) {
    namespaceURI = "";
} else {
    final XmlSchema xmlSchemaAnnotation = targetPackage.getAnnotation(XmlSchema.class);
    if (xmlSchemaAnnotation == null) {
        namespaceURI = "";
    } else {
...
QNamegetNamespace(Map namespaces, String str, String defaultNamespace)
get Namespace
String prefix = null;
String localName = null;
StringTokenizer tokenizer = new StringTokenizer(str, ":");
if (tokenizer.countTokens() == 2) {
    prefix = tokenizer.nextToken();
    localName = tokenizer.nextToken();
} else if (tokenizer.countTokens() == 1) {
    localName = tokenizer.nextToken();
...
NamespaceContextgetNamespaceContext()
get Namespace Context
return namespaceContext;
NamespaceContextgetNamespaceContext(final String prefix, final String uri)
get Namespace Context
return new NamespaceContext() {
    @Override
    public String getNamespaceURI(String prefix) {
        return uri;
    @Override
    public String getPrefix(String uri) {
        return prefix;
...
NamespaceContextgetNamespaceCtx()
get Namespace Ctx
return new NamespaceContext() {
    @Override
    public String getNamespaceURI(String prefix) {
        switch (prefix) {
        case "asic":
            return NS_ASIC;
        case "ds":
            return NS_DS;
...
ListgetNamespacesForClass(final Class clazz)
get Namespaces For Class
final List<String> namespaces = new ArrayList<String>();
final XmlSeeAlso seeAlso = clazz.getAnnotation(XmlSeeAlso.class);
if (seeAlso != null) {
    for (final Class<?> c : seeAlso.value()) {
        namespaces.addAll(getNamespacesForClass(c));
return namespaces;
...
StringgetNamespaceURI(Name name)
get Namespace URI
String uri = name.getURI();
return uri == null || uri.length() == 0 ? null : uri;
StringgetPackageNamespace(Class cls)
get Package Namespace
Package p = cls.getPackage();
if (p != null) {
    javax.xml.bind.annotation.XmlSchema schemaAnn = p
            .getAnnotation(javax.xml.bind.annotation.XmlSchema.class);
    if (schemaAnn != null) {
        return schemaAnn.namespace();
return null;
StringgetPrefix(final Package targetPackage, String namespaceURI)
get Prefix
String prefix;
final Map<String, String> namespacePrefixes = new HashMap<String, String>();
if (targetPackage != null) {
    final XmlSchema xmlSchemaAnnotation = targetPackage.getAnnotation(XmlSchema.class);
    if (xmlSchemaAnnotation != null) {
        for (XmlNs xmlns : xmlSchemaAnnotation.xmlns()) {
            namespacePrefixes.put(xmlns.namespaceURI(), xmlns.prefix());
prefix = namespacePrefixes.get(namespaceURI);
return prefix;