Skip to main content

namespace definitions in XML output using JAXB2.0

4 replies [Last post]
ramazanyich2
Offline
Joined: 2005-09-05
Points: 0

I have two separate packages
com.company1.doc
and com.company2.doc

every package contains namespace definition (in package-info.java):
@javax.xml.bind.annotation.XmlSchema(namespace = "http://company1.com/doc")
package com.company1.doc;

and
@javax.xml.bind.annotation.XmlSchema(namespace = "http://company2.com/doc")
package com.company2.doc;

I initialize JAXBContext using JAXBContext.newInstance("com.company1:com.company2");

Then I construct java objects in both packages:

Problem is then I unmarshall documents of one package I see namespace of another package:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

...

Is it possible to avoid printing of unused namespace ?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ramazanyich2
Offline
Joined: 2005-09-05
Points: 0

added as issue 103

ramazanyich2
Offline
Joined: 2005-09-05
Points: 0

I tried with latest version of JAXB and it is still don't work. I attached my class files to the issue 103

ramazanyich2
Offline
Joined: 2005-09-05
Points: 0

you can say that I can use separate JAXB contexts per every package.
But I want to make it from one JAXBContext.
Because some classes of one package use classes from another package and some not.

kohsuke
Offline
Joined: 2003-06-09
Points: 0

The default printer of the JAXB RI prints out all "known" namespaces upfront. This is because it generally improves the performance.

One quick way around the problem would be for you to use a custom writer and filter out things that you think unnecessary. For example, you can marshal it to DOM, remove some unused namespace declarations, and write the DOM tree out. Or you can implement a SAX filter if you need more performance than DOM.

Allowing lazy namespace declarations inside the RI is bit tricky, so I need more thinking. If you feel strongly that the above workarounds I suggested isn't satisfactory, you are welcome to file an issue on http://jaxb.dev.java.net/ to track the progress.