Juan Martin Desimoni
Juan Martin Desimoni

Reputation: 186

Is there any way to obtain the XSD type given a Java Type?

I'm working with JavaTypes and I want to transform a JavaType to XsdType. I did not found a simple way to do this, and finally created an method that given a JavaType returns the associated XsdType.

Example:

toXsdType(String.class.getName()); --> "xsd:string"

In order to achieve this, I'm doing an UGLY switch statement.

Is there a clean way to do it, using JAXB or another library?

Upvotes: 2

Views: 43

Answers (1)

lexicore
lexicore

Reputation: 43671

You might be interested in TypeInfoSet.getTypeInfo(T type) which gives you an XML type for the given Java type.

As for the TypeInfoSet, you can obtain it via JAXBContextImpl.getTypeInfoSet().

So basically the whole process would look like:

  • Create an instance of JAXBContext with RI, cast it to JAXBContextImpl.
  • Get a TypeInfoSet.
  • Get a NonElement for your Java type.
  • Get the QName using NonElement.getTypeName().

Upvotes: 4

Related Questions