简体   繁体   English

给定Java类型,是否可以获取XSD类型?

[英]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. 我正在使用JavaTypes,我想将JavaType转换为XsdType。 I did not found a simple way to do this, and finally created an method that given a JavaType returns the associated XsdType. 我没有找到执行此操作的简单方法,最后创建了一个方法,该方法给定JavaType返回关联的XsdType。

Example: 例:

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

In order to achieve this, I'm doing an UGLY switch statement. 为了实现这一点,我正在做一个UGLY switch语句。

Is there a clean way to do it, using JAXB or another library? 有使用JAXB或其他库的干净方法吗?

You might be interested in TypeInfoSet.getTypeInfo(T type) which gives you an XML type for the given Java type. 您可能对TypeInfoSet.getTypeInfo(T type)感兴趣, TypeInfoSet.getTypeInfo(T type)为您提供给定Java类型的XML类型。

As for the TypeInfoSet , you can obtain it via JAXBContextImpl.getTypeInfoSet() . 至于TypeInfoSet ,可以通过JAXBContextImpl.getTypeInfoSet()获得它。

So basically the whole process would look like: 所以基本上整个过程看起来像:

  • Create an instance of JAXBContext with RI, cast it to JAXBContextImpl . 使用RI创建一个JAXBContext实例,将其JAXBContextImplJAXBContextImpl
  • Get a TypeInfoSet . 获取一个TypeInfoSet
  • Get a NonElement for your Java type. 为您的Java类型获取NonElement
  • Get the QName using NonElement.getTypeName() . 使用NonElement.getTypeName()获得QName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM