• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXB namespace

 
Joyce Kind
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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!
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ivan Krizsan
Ranch Hand
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic