Skip to main content

How Do I remove namespace declarations from child elements

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
dd0
Offline
Joined: 2011-03-17

I have a project where the external vendor crashes when encountering namespace declaration attributes on Elements. JAXB is creating xmlns:xs ... tags on JAXBElement types. I need a way to remove the namespace decl attributes in order to work with this essential trading partner.
The Element that gets marshalled is:
<Comments>
<Comment GuestViewable="true">
<Text xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Special Request: SPECIAL, SO SPECIAL.</Text>
</Comment>
<Comment GuestViewable="true">
<Text xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">Arrival Information: I WILL FLY IN ON MY MAGIC CARPET STRAIGHT THROUGH THE BALCONY WINDOW.</Text>
</Comment>
</Comments>
Note the xs and xsi generated attributes on the Text element. The code I use to bind this to the Jaxb generated object is:
CommentType.Comment comment = new CommentType.Comment();
comment.setGuestViewable(true);
JAXBElement textElem =
new JAXBElement(new QName("http://www.opentravel.org/OTA/2003/05",
"Text",
""),
CommentType.Comment.class,
commentText);
comment.getTextOrImageOrURL().add(textElem);
commentType.getComment().add(comment);

I have read this and the otn forums and have had no success in finding a succinct and workable solution to stripping out the namespace attributes from this element. Any help is greatly appreciated.

Reply viewing options

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

The xsi:type declarations are most likely being created because the declared type of the JAXBElement does not match the type of the value.

If the ObjectFactory has a create method for the correct JAXBElement you should use that since it should correctly populate both the QName and the type info; otherwise I would try setting the declared type (second constructor arg) of the JAXBElement to String.class (assuming this is the type of commentTest) instead of CommentType.Comment.

dd0
Offline
Joined: 2011-03-17

cbrettin,
you were right about the type of the JAXBElement. The CommentType.Comment's value parameter is a List<JAXBElement<?>>. I neglected to declare the jaxb element's type parameter. (which was not String.class, but another jaxb generated type called FormattedTextTextType). This particular construct was generated because the underlying schema typed this as a sequence with one or more occurrences of four possible values each of a different type.
Your suggestion was right on. It pulled me out of the weeds of NamespacePrefixHandlers which weren't applicable to my problem.
I don't see how to rate you, but once I get that figured out, I'll definitely mark you the highest. Thanks again.
Cheers! (Erin Go Bragh)