wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB namespace" Watch "JAXB namespace" New topic
Author

JAXB namespace

Joyce Kind
Greenhorn

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

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

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?

Thanks.
Ivan Krizsan
Ranch Hand

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


My free books and tutorials: http://www.slideshare.net/krizsan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB namespace
 
Similar Threads
Discrepancies between an XML file and the XSD it was generated from (via JAXB)
Why namespace information is not in attributes list
Displaying name space for each element in the xml generated using DOM
Using JAX-WS with JAXB problem
Get namespace of root element