Multiple namespace package info Java
Asked Answered
D

2

6

I have this package info

/**
 * Created by mflamant on 13/02/2017.
 */
@javax.xml.bind.annotation.XmlSchema(namespace = "namespace1", xmlns = {@XmlNs(prefix = "ns4", namespaceURI = "namespace1")}, elementFormDefault = XmlNsForm.QUALIFIED)

package com.cisco.adt.portal.data.model.API.Equipment;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;

But I want to have 2 namespaces, but I tried to do this:

/**
 * Created by mflamant on 13/02/2017.
 */
@javax.xml.bind.annotation.XmlSchema(namespace = "namespace1", xmlns = {@XmlNs(prefix = "ns4", namespaceURI = "namespace1")}, elementFormDefault = XmlNsForm.QUALIFIED)
@javax.xml.bind.annotation.XmlSchema(namespace = "namespace2", xmlns = {@XmlNs(prefix = "ns4", namespaceURI = "namespace2")}, elementFormDefault = XmlNsForm.QUALIFIED)

package com.cisco.adt.portal.data.model.API.Equipment;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;

But I have an error: "Duplicate annotation", is this possible to have 2 namespaces or not ?

Thank You.

Drive answered 13/2, 2017 at 12:26 Comment(0)
N
8

You can have more namespaces but not with the same prefix.

Instead of using the annotation directly into your class, I suggest to add package-info.java file in the package where your model are.

For example, once I had to build a sitemap where I needed to add more namespaces, because of the strict checking rules of google search console.

Inside the package-info.java file I added two namespaces with the following syntax.

@XmlSchema(
    xmlns = { 
        @XmlNs(prefix = "video", namespaceURI = "http://www.google.com/schemas/sitemap-video/1.1"),
        @XmlNs(prefix = "", namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9")
    }
)

/*
 * xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
 * xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"
 */

package com.example.myapplication.model.sitemap.pojo;

import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlSchema;
Nevil answered 13/2, 2017 at 12:41 Comment(2)
Thank you ! This is it !Drive
you didn't define any namespace in @XmlSchema you define only namespaceURI. I want to define two different namespaces with different prefixToffee
A
1

You should use an array of annotations instead as per the documentation:

https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/XmlSchema.html

e.g:

@javax.xml.bind.annotation.XmlSchema (
      xmlns = {
        @javax.xml.bind.annotation.XmlNs(prefix = "po",
                   namespaceURI="http://www.example.com/myPO1"),

        @javax.xml.bind.annotation.XmlNs(prefix="xs",
                   namespaceURI="http://www.w3.org/2001/XMLSchema")
      }
    )

The java documentation does have an erroroneous parentheses at the end of the xmlns block, which I have corrected in the example above.

Angele answered 13/2, 2017 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.