Skip to main content

"@XmlAnyElement Object" & "@XmlJavaTypeAdapter"

5 replies [Last post]
ratoo
Offline
Joined: 2007-08-27
Points: 0

Hello everybody,

Is it possible to adapt

<br />
@XmlAnyElement Object object;<br />

?

As far as I understand the type is obtained through a reflection when JAXBContext is created and not at runtime?

Ratoo

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
grpahicpoet
Offline
Joined: 2006-06-09
Points: 0

Not following you. @XmlAnyElement, as far as I can tell, is expecting a collection or list type to accept any child elements which don't have individual, typed members in this class.

ratoo
Offline
Joined: 2007-08-27
Points: 0

Hello,

Not exactly... "It typically annotates a multi-valued JavaBean property, but it can occur on single value JavaBean property."

In my case I am catching virtually "any" element.
In case of catching org.w3c.dom.Element I decode it by hand... otherwise I get usual JAXB object... maybe there is another way to achieve it?

Thank you,

Ratoo

becomingguru
Offline
Joined: 2008-08-13
Points: 0

I'm still not following you.

Looks to me like your going to get DOM Elements in your collection which you annotate with @XmlAnyElement. The JAXBContext doesn't know anything about the types which may be present in any random xml document which you decide to unmarshall, so it just hands you elements.

ratoo
Offline
Joined: 2007-08-27
Points: 0

It gives me elements only if it can't find an appropriate jaxb object.

[code]
@XmlAnyElement(lax = true)
Object getJaxbObject() {...}
void setJaxbObject(Object object) {...}
[/code]

works perfectly...

the problem is that it looks that JAXB injects an adapter looking into method signature in a time of jaxb context creation (?) and not in runtime when it actually sets the object.

Imagine I have a bean:

@XmlJavaTypeAdapter(MyAdapter.class)
public class MyBean
{
}

It supposed that wherever JAXB find a MyBean it must use MyAdapter to marshal/unmarshal it.

But in case of Object it is no so... I can not adapt arbitrary... sadly,

Cheers,

Ratoo

becomingguru
Offline
Joined: 2008-08-13
Points: 0

perhaps you want javax.xml.bind.Marshaller.setAdapter(Class type, A adapter)