Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

JAXB namespace

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using JAXB2 marshaller to create an xml file based on the generated classes from the xml schema. I got two questions regarding the namespace:

1. In the root element "ROOT", I need to add attributes xsi:schemaLoaction, xmlns, and xmlns:xsi as below
<?xml version="1.0" encoding="UTF-8" ?>
<ROOT jobname="job"
xsi:schemaLocation="...."
xmlns="........"
xmlns:xsi="..............">....
</ROOT>

But from the generated class ROOT, I am not able to find the setSchemaLocation() or SetXmlns(), How do I set these values to ROOT element?

2. There is another element class that has setCity() method, and this method takes JAXBElement<String> as argument. I created the JAXBElement<String> as below:

JAXBElement<String> city = new JAXBElement<String>(new QName("city"), String.class, null, "New York"); //cannot get rid of namespace
address.setCity(city);

And the result xml is
<city xmlns:ns2="http://www.mycom.com/XMLSchema/myxsd" xmlns="">New York</city>

I need <city>New York</city>, and how can I get rid of the namespace?

Thanks very much!
 
Marshal
Posts: 27276
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know the answer to (1). But the answer to (2) is that you don't have to do anything. In both examples you have a <city> element which is not in any namespace. It's true that one of them also has an extra namespace declaration, but that doesn't apply to anything in the element. Your two examples are functionally identical.
 
Joyce Kind
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul,

So <city>New York</city> and <city xmlns:ns2="http://www.mycom.com/XMLSchema/myxsd" xmlns="">New York</city> are functionally the same.

I would like to get rid of the automatically generated xmlns:ns2 part, as our sample client xml does not include this. How can I do that?

Thanks.
 
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
If the reason for setting the schema location is that you want to perform validation against the XML schema when marshalling or unmarshalling, then setting the schema should be done programmatically using the Marshaller or Unmarshaller classes.
Example:

Best wishes!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic