users@jaxb.java.net

RE: How to get XML name of attributes?

From: Markus Karg <karg_at_quipsy.de>
Date: Wed, 11 May 2011 08:47:18 +0200

Thank you for your kind help. Unfortunately this is not working. The methods described below to not exist in Java 6, so I tried getTypeInfoSet.getClassInfo(Bar.class) but that bears no getProperty method. :-(

It would be so easy if JAXBIntrospector would have a method getXmlAttributeName(String beanPropertyName)...

-----Original Message-----
From: aleksei.valikov_at_gmail.com [mailto:aleksei.valikov_at_gmail.com] On Behalf Of Aleksei Valikov
Sent: Mittwoch, 11. Mai 2011 08:39
To: users_at_jaxb.java.net
Subject: Re: How to get XML name of attributes?

Hi,

> I have some JAXB classes like this one:
>
> @XmlElementRoot(name = "foo")
>
> public class Bar {
>
>    @XmlAttribute(name = "xxx")
>
>    public String test;
>
> }
>
>
>
> With the word "test" in my hands, I need to get "xxx".
>
>
>
> In other words: How to find out into what attribute name JAXB would marshal
> this property?
>
>
>
> I certainly could use Reflection
> (Bar.class.getMethod("test").getAnnotation(XmlAttribute.class)), but this
> might get tricky in case that annotation is not given -- my code would have
> to repeat the exact rules for JAXB default naming then...
>
>
>
> So if someone knows some JAXB trick to get this out of the box, I would be
> really happy! :-)

The ultimate thing would be to use JAXB Models, namely
com.sun.xml.bind.v2.model.runtime.RuntimeTypeInfoSet. It has the
complete information of the JAXB model used for marshalling and
unmarshalling.

Brief outline:

* Create com.sun.xml.bind.v2.runtime.JAXBContextImpl or cast to it
from JAXBContext (JAXB RI only)
* context.getTypeInfoSet()
* typeInfoSet.getType(Bar.class)
* classInfo.getProperty("test")
* attributePropertyInfo.getXmlName() is what you need

Very adult usage of JAXB.

Bye,
/lexi