Skip to main content

JAXB namespace problem = Urgent

7 replies [Last post]
rajakumar
Offline
Joined: 2005-04-26

Hi,
I am trying to marshal java object into XML using JAXB.

The EXPECTED result is

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

ABCD

1234

1234567

XYZ

PQR

but the ACTUAL result I am getting is as follows

?xml version="1.0" encoding="UTF-8"?>

ABCD

1234

1234567

XYZ

PQR

That is, it does not pick up the correct NAMESPACE names such as namespace1 and namespace2, instead it gives its own namespaces names such as ns1, ns2 and so on.

Please help me to solve this problem.

Raja

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
crabi_77
Offline
Joined: 2006-05-05

Hi kohsuke,
I have gone through all the examples still same problem, I am using JAXB 2.1.x
and marshal and unmarshl will not give any problem but I have to remove ns2 in my xml request and response, and I have used NamespacePrefixMapper also see the below example.. where i am doing wrong..

Regards
Rabindra

philipj
Offline
Joined: 2009-04-28

Hi Guys did you solve the problem?
Philip

Martin Grebac

Hi,
I'd suggest to look at my tech tip which includes source code as well:
http://blogs.sun.com/enterprisetechtips/entry/customizing_jaxb
If you still think your code is right and prefix mapper is not used
correctly, please file an issue.
Thanks,
MartinG

metro@javadesktop.org wrote:
> Hi Guys did you solve the problem?
> Philip
> [Message sent by forum member 'philipj' (philipj)]
>
> http://forums.java.net/jive/thread.jspa?messageID=344249
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
> For additional commands, e-mail: users-help@metro.dev.java.net
>
>

--
Martin Grebac, http://blogs.sun.com/mgrebac

Web Technologies & Standards
Sun Microsystems Czech

ICQ: 93478885

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@metro.dev.java.net
For additional commands, e-mail: users-help@metro.dev.java.net

mindchi
Offline
Joined: 2007-03-26

Hi Martin,

Any workarounds to the problem identified in issue 103 reproduced below.

Excuse the tone, but I am pretty annoyed with this issue.

Thanks.

-tbb

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:



...

Is it possible to avoid printing of unused namespace ?------- Additional comments from kohsuke Fri Oct 28 19:19:14 +0000 2005 -------

Hmm.

Would it be possible for you to try the latest nightly build? I remember fixing
an issue like this some time ago, and I couldn't reproduce the problem with the
current snapshot of the JAXB RI.

If it still doesn't work, please reopen the bug so that it gets our attention.------- Additional comments from ramazanyich2 Tue Nov 8 17:42:27 +0000 2005 -------

Created an attachment (id=57)
test class file which produces output
------- Additional comments from ramazanyich2 Tue Nov 8 17:43:08 +0000 2005 -------

Created an attachment (id=58)
class file for first package
------- Additional comments from ramazanyich2 Tue Nov 8 17:43:55 +0000 2005 -------

Created an attachment (id=59)
class file for second package
------- Additional comments from ramazanyich2 Tue Nov 8 17:44:19 +0000 2005 -------

Created an attachment (id=60)
package info for second package
------- Additional comments from ramazanyich2 Tue Nov 8 17:44:44 +0000 2005 -------

Created an attachment (id=61)
package info for first package
------- Additional comments from ramazanyich2 Tue Nov 8 17:45:20 +0000 2005 -------

Created an attachment (id=62)
result of execution
------- Additional comments from ramazanyich2 Tue Nov 8 17:48:46 +0000 2005 -------

I tried it with lates build ( dated by 8 november) but in result I still have
all namespaces.
I attached all files to the issue. Run testJaxb.java and you will have test.xml
as result.
It will have xmlns:ns3="http://company2.com">
xmlns:ns3="http://company2.com" is unwanted as object TestClass2 was not created.------- Additional comments from ktrapszo Wed Apr 19 19:34:20 +0000 2006 -------

Using 4/19/2006 build, this is still an issue. ------- Additional comments from kohsuke Wed May 31 01:16:59 +0000 2006 -------

This is actually the expected behavior.

In 1.0, we did what ramazanyich2 wanted us to do --- namely, declare namespaces
lazily on-demand, only when it's necessary. Often this results in namespaces
declared multiple places in sub-trees, and many people weren't happy with this.

We can't make it any smarter, as in short of traversing the whole object tree,
The marshaller won't be able to discover all the namespaces in use. Such
traversal would be very costly.

So in 2.0 we decided to change the behavior to always declare all the statically
known namespaces upfront. This also made the namespace management inside the
marshaller simpler, contributed to the overall performance improvement.

Given those background, at this point we are not planning to change this behavior.------- Additional comments from mindchi Thu Apr 1 15:38:22 +0000 2010 -------

I'm requesting that this issue be reopened as having the extraneous namespaces
causes problems when using JAXB as the data binding framework for CXF. Firstly,
I think it is unacceptable to have a solution which changes your data. If I
take an XML file read it in and write it back out, I should end up with exactly
the same file. Not a file that has extra stuff in it. This reason alone should
be enough to warrant a fix.

I am writing some web service code using CFX and JAXB. I check my code all the
way to the point where I include it as an element of a larger document that
gets passed into CFX framework for handling web service processing. When it
comes out, it has extra tags in it. It is sent to publish and subscribe server.
When I try to read the data when I retrieve it from the server, CXF produces a
SAX parse exception due to one of the extraneous namespaces which it wasn't
expecting. Now, it may be possible for me to intercept the data and remove the
extranuous namespace definitions, but this is an enterprise system, and I
cannot expect every other application to do this. Up until this point, my
experience with JAXB has been good. I like the performance, however, this issue
basically renders JAXB useless to me on this project. I would like to continue
to use JAXB, however, I don't expect this to be fixed anytime soon, so I will
need to look into using some other data binding framework, perhaps XMLBeans
with CXF.

Please consider correcting this problem so I can continue using JAXB in
conjunction with CXF.

hossionrifat
Offline
Joined: 2010-05-26

I think the problem is with the namespaces. The produced XML does not have a default namespace only the schema namespace associated to a prefix. All elements are prefixed with this prefix except the root element, which results in failing the validation.I have an element in my DTD which contains a choice of elemnts within it. This translates into a getContent method in the generated class, due to the fact that jaxb doesn't know which element it will actually find inside. This is fine. The problem is, while i get a getContent method, there is

[url=http://www.carpartswarehouse.com/carmodels/CP16/Lexus/LX570.html]Lexus LX570 Parts[/url]

kohsuke
Offline
Joined: 2003-06-09

Looks like you are using 1.0.x. This is a forum for 2.0 discussions. Please direct 1.0 issues to users@jaxb.dev.java.net (subscribe from http://jaxb.dev.java.net/)

If you'd like to control prefixes, use NamespacePrefixMapper. See one of the bundled samples for the usage, and also search the JAXB users list archive.

crabi_77
Offline
Joined: 2006-05-05

Hi,
I am also facing same problem, how we can remove ns2 issues in jaxb.
i have tested all the example still i am not able to solve.
Able to marshal and unmarshal xml content but i want send the response without any namespace. I am using xmlFilter for marshal and unmarsal. please help

static class NamespacePrefixMapperImpl extends com.sun.xml.bind.marshaller.NamespacePrefixMapper{
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
if( namespaceUri.equals("http://www.targetnamespaceurl/schemas/")) {
return (requirePrefix) ? "ns2" : "";
}
return suggestion;
}
};
i am using above example still it is not resolved.

Regards
Rabi