wood burning stoves
The moose likes Web Services and the fly likes JAXB namespace Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB namespace" Watch "JAXB namespace" New topic

JAXB namespace

Joyce Kind

Joined: Nov 27, 2008
Posts: 8

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"

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

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

Joined: Oct 14, 2005
Posts: 19973

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

Joined: Nov 27, 2008
Posts: 8
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?

Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
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.

Best wishes!

My free books and tutorials: http://www.slideshare.net/krizsan
I agree. Here's the link: http://aspose.com/file-tools
subject: JAXB namespace
It's not a secret anymore!