jaxb,全称为Java Architecture for Xml Binding,是一种将java对象与xml建立起映射的技术。其主要提供两个功能,一是将java对象映射为xml,二是将xml映射为java对象。JAXB有1.0版和2.0版。2.0版对应的JSR(Java specification request, java规格要求)是JSR 222。jaxb中的xjc工具能够将XML Schema转换为对应的java类。支持的XML类型包括XML DTD,XSD以及WSDL。而schemagen工具则可以将具有相应annotation标记的java类转换为XML结构。

ant脚本有xjc插件来实现对xml schema文件转换为java类的工作。而由于ant任务是gradle中的一等公民,所以我们可以直接在gradle脚本中使用ant的xjc插件来实现对xml schema和java类的映射。以下代码演示了如何将xsd格式和wsdl格式的xml转换为具体的java类。

build.gradle
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  1. configurations {
  2. jaxb
  3. }
  4. dependencies {
  5. jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7'
  6. jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7'
  7. }
  8. ext {
  9. generatedSourceDir = 'src/main/generated'
  10. }
  11. task jaxb {
  12. doLast {
  13. file(generatedSourceDir).mkdirs()
  14. ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.jaxb.asPath)
  15. ant.xjc(destdir: generatedSourceDir,
  16. package: 'jaxb.ws.ship',
  17. schema: 'schema/shiporder.xsd'
  18. )
  19. ant.xjc(destdir: generatedSourceDir,
  20. package: 'jaxb.ws.hello',
  21. schema: 'schema/weather.wsdl'
  22. ) {
  23. arg(value: '-wsdl')
  24. }
  25. }
  26. }
  27. clean {
  28. ant.delete(dir: generatedSourceDir)
  29. }

这里实现了将xsd和wsdl格式的xml文档转换为具体的java类。注意一点是如果wsdl中的schema过于简单,可能不会有具体的类生成。另外附上使用的示例文件。

shiporder.xsd文件如下:

shiporder.xsd
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  3. <xs:element name="shiporder">
  4. <xs:complexType>
  5. <xs:sequence>
  6. <xs:element name="orderperson" type="xs:string"/>
  7. <xs:element name="shipto">
  8. <xs:complexType>
  9. <xs:sequence>
  10. <xs:element name="name" type="xs:string"/>
  11. <xs:element name="address" type="xs:string"/>
  12. <xs:element name="city" type="xs:string"/>
  13. <xs:element name="country" type="xs:string"/>
  14. </xs:sequence>
  15. </xs:complexType>
  16. </xs:element>
  17. <xs:element name="item" maxOccurs="unbounded">
  18. <xs:complexType>
  19. <xs:sequence>
  20. <xs:element name="title" type="xs:string"/>
  21. <xs:element name="note" type="xs:string" minOccurs="0"/>
  22. <xs:element name="quantity" type="xs:positiveInteger"/>
  23. <xs:element name="price" type="xs:decimal"/>
  24. </xs:sequence>
  25. </xs:complexType>
  26. </xs:element>
  27. </xs:sequence>
  28. <xs:attribute name="orderid" type="xs:string" use="required"/>
  29. </xs:complexType>
  30. </xs:element>
  31. </xs:schema>

weather.wsdl文件内容如下:

weather.wsdl
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  116. 116
  117. 117
  118. 118
  119. 119
  120. 120
  121. 121
  122. 122
  123. 123
  124. 124
  125. 125
  126. 126
  127. 127
  128. 128
  129. 129
  130. 130
  131. 131
  132. 132
  133. 133
  134. 134
  135. 135
  136. 136
  137. 137
  138. 138
  139. 139
  140. 140
  141. 141
  142. 142
  143. 143
  144. 144
  145. 145
  146. 146
  147. 147
  148. 148
  149. 149
  150. 150
  151. 151
  152. 152
  153. 153
  154. 154
  155. 155
  156. 156
  157. 157
  158. 158
  159. 159
  160. 160
  161. 161
  162. 162
  163. 163
  164. 164
  165. 165
  166. 166
  167. 167
  168. 168
  169. 169
  170. 170
  171. 171
  172. 172
  173. 173
  174. 174
  175. 175
  176. 176
  177. 177
  178. 178
  179. 179
  180. 180
  181. 181
  182. 182
  183. 183
  184. 184
  185. 185
  186. 186
  187. 187
  188. 188
  189. 189
  190. 190
  191. 191
  192. 192
  193. 193
  194. 194
  195. 195
  196. 196
  197. 197
  198. 198
  199. 199
  200. 200
  201. 201
  202. 202
  203. 203
  204. 204
  205. 205
  206. 206
  207. 207
  208. 208
  209. 209
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <wsdl:definitions xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.webserviceX.NET" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.webserviceX.NET" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  3. <wsdl:types>
  4. <s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET">
  5. <s:element name="GetWeather">
  6. <s:complexType>
  7. <s:sequence>
  8. <s:element minOccurs="0" maxOccurs="1" name="CityName" type="s:string" />
  9. <s:element minOccurs="0" maxOccurs="1" name="CountryName" type="s:string" />
  10. </s:sequence>
  11. </s:complexType>
  12. </s:element>
  13. <s:element name="GetWeatherResponse">
  14. <s:complexType>
  15. <s:sequence>
  16. <s:element minOccurs="0" maxOccurs="1" name="GetWeatherResult" type="s:string" />
  17. </s:sequence>
  18. </s:complexType>
  19. </s:element>
  20. <s:element name="GetCitiesByCountry">
  21. <s:complexType>
  22. <s:sequence>
  23. <s:element minOccurs="0" maxOccurs="1" name="CountryName" type="s:string" />
  24. </s:sequence>
  25. </s:complexType>
  26. </s:element>
  27. <s:element name="GetCitiesByCountryResponse">
  28. <s:complexType>
  29. <s:sequence>
  30. <s:element minOccurs="0" maxOccurs="1" name="GetCitiesByCountryResult" type="s:string" />
  31. </s:sequence>
  32. </s:complexType>
  33. </s:element>
  34. <s:element name="string" nillable="true" type="s:string" />
  35. </s:schema>
  36. </wsdl:types>
  37. <wsdl:message name="GetWeatherSoapIn">
  38. <wsdl:part name="parameters" element="tns:GetWeather" />
  39. </wsdl:message>
  40. <wsdl:message name="GetWeatherSoapOut">
  41. <wsdl:part name="parameters" element="tns:GetWeatherResponse" />
  42. </wsdl:message>
  43. <wsdl:message name="GetCitiesByCountrySoapIn">
  44. <wsdl:part name="parameters" element="tns:GetCitiesByCountry" />
  45. </wsdl:message>
  46. <wsdl:message name="GetCitiesByCountrySoapOut">
  47. <wsdl:part name="parameters" element="tns:GetCitiesByCountryResponse" />
  48. </wsdl:message>
  49. <wsdl:message name="GetWeatherHttpGetIn">
  50. <wsdl:part name="CityName" type="s:string" />
  51. <wsdl:part name="CountryName" type="s:string" />
  52. </wsdl:message>
  53. <wsdl:message name="GetWeatherHttpGetOut">
  54. <wsdl:part name="Body" element="tns:string" />
  55. </wsdl:message>
  56. <wsdl:message name="GetCitiesByCountryHttpGetIn">
  57. <wsdl:part name="CountryName" type="s:string" />
  58. </wsdl:message>
  59. <wsdl:message name="GetCitiesByCountryHttpGetOut">
  60. <wsdl:part name="Body" element="tns:string" />
  61. </wsdl:message>
  62. <wsdl:message name="GetWeatherHttpPostIn">
  63. <wsdl:part name="CityName" type="s:string" />
  64. <wsdl:part name="CountryName" type="s:string" />
  65. </wsdl:message>
  66. <wsdl:message name="GetWeatherHttpPostOut">
  67. <wsdl:part name="Body" element="tns:string" />
  68. </wsdl:message>
  69. <wsdl:message name="GetCitiesByCountryHttpPostIn">
  70. <wsdl:part name="CountryName" type="s:string" />
  71. </wsdl:message>
  72. <wsdl:message name="GetCitiesByCountryHttpPostOut">
  73. <wsdl:part name="Body" element="tns:string" />
  74. </wsdl:message>
  75. <wsdl:portType name="GlobalWeatherSoap">
  76. <wsdl:operation name="GetWeather">
  77. <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get weather report for all major cities around the world.</wsdl:documentation>
  78. <wsdl:input message="tns:GetWeatherSoapIn" />
  79. <wsdl:output message="tns:GetWeatherSoapOut" />
  80. </wsdl:operation>
  81. <wsdl:operation name="GetCitiesByCountry">
  82. <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get all major cities by country name(full / part).</wsdl:documentation>
  83. <wsdl:input message="tns:GetCitiesByCountrySoapIn" />
  84. <wsdl:output message="tns:GetCitiesByCountrySoapOut" />
  85. </wsdl:operation>
  86. </wsdl:portType>
  87. <wsdl:portType name="GlobalWeatherHttpGet">
  88. <wsdl:operation name="GetWeather">
  89. <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get weather report for all major cities around the world.</wsdl:documentation>
  90. <wsdl:input message="tns:GetWeatherHttpGetIn" />
  91. <wsdl:output message="tns:GetWeatherHttpGetOut" />
  92. </wsdl:operation>
  93. <wsdl:operation name="GetCitiesByCountry">
  94. <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get all major cities by country name(full / part).</wsdl:documentation>
  95. <wsdl:input message="tns:GetCitiesByCountryHttpGetIn" />
  96. <wsdl:output message="tns:GetCitiesByCountryHttpGetOut" />
  97. </wsdl:operation>
  98. </wsdl:portType>
  99. <wsdl:portType name="GlobalWeatherHttpPost">
  100. <wsdl:operation name="GetWeather">
  101. <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get weather report for all major cities around the world.</wsdl:documentation>
  102. <wsdl:input message="tns:GetWeatherHttpPostIn" />
  103. <wsdl:output message="tns:GetWeatherHttpPostOut" />
  104. </wsdl:operation>
  105. <wsdl:operation name="GetCitiesByCountry">
  106. <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">Get all major cities by country name(full / part).</wsdl:documentation>
  107. <wsdl:input message="tns:GetCitiesByCountryHttpPostIn" />
  108. <wsdl:output message="tns:GetCitiesByCountryHttpPostOut" />
  109. </wsdl:operation>
  110. </wsdl:portType>
  111. <wsdl:binding name="GlobalWeatherSoap" type="tns:GlobalWeatherSoap">
  112. <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
  113. <wsdl:operation name="GetWeather">
  114. <soap:operation soapAction="http://www.webserviceX.NET/GetWeather" style="document" />
  115. <wsdl:input>
  116. <soap:body use="literal" />
  117. </wsdl:input>
  118. <wsdl:output>
  119. <soap:body use="literal" />
  120. </wsdl:output>
  121. </wsdl:operation>
  122. <wsdl:operation name="GetCitiesByCountry">
  123. <soap:operation soapAction="http://www.webserviceX.NET/GetCitiesByCountry" style="document" />
  124. <wsdl:input>
  125. <soap:body use="literal" />
  126. </wsdl:input>
  127. <wsdl:output>
  128. <soap:body use="literal" />
  129. </wsdl:output>
  130. </wsdl:operation>
  131. </wsdl:binding>
  132. <wsdl:binding name="GlobalWeatherSoap12" type="tns:GlobalWeatherSoap">
  133. <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
  134. <wsdl:operation name="GetWeather">
  135. <soap12:operation soapAction="http://www.webserviceX.NET/GetWeather" style="document" />
  136. <wsdl:input>
  137. <soap12:body use="literal" />
  138. </wsdl:input>
  139. <wsdl:output>
  140. <soap12:body use="literal" />
  141. </wsdl:output>
  142. </wsdl:operation>
  143. <wsdl:operation name="GetCitiesByCountry">
  144. <soap12:operation soapAction="http://www.webserviceX.NET/GetCitiesByCountry" style="document" />
  145. <wsdl:input>
  146. <soap12:body use="literal" />
  147. </wsdl:input>
  148. <wsdl:output>
  149. <soap12:body use="literal" />
  150. </wsdl:output>
  151. </wsdl:operation>
  152. </wsdl:binding>
  153. <wsdl:binding name="GlobalWeatherHttpGet" type="tns:GlobalWeatherHttpGet">
  154. <http:binding verb="GET" />
  155. <wsdl:operation name="GetWeather">
  156. <http:operation location="/GetWeather" />
  157. <wsdl:input>
  158. <http:urlEncoded />
  159. </wsdl:input>
  160. <wsdl:output>
  161. <mime:mimeXml part="Body" />
  162. </wsdl:output>
  163. </wsdl:operation>
  164. <wsdl:operation name="GetCitiesByCountry">
  165. <http:operation location="/GetCitiesByCountry" />
  166. <wsdl:input>
  167. <http:urlEncoded />
  168. </wsdl:input>
  169. <wsdl:output>
  170. <mime:mimeXml part="Body" />
  171. </wsdl:output>
  172. </wsdl:operation>
  173. </wsdl:binding>
  174. <wsdl:binding name="GlobalWeatherHttpPost" type="tns:GlobalWeatherHttpPost">
  175. <http:binding verb="POST" />
  176. <wsdl:operation name="GetWeather">
  177. <http:operation location="/GetWeather" />
  178. <wsdl:input>
  179. <mime:content type="application/x-www-form-urlencoded" />
  180. </wsdl:input>
  181. <wsdl:output>
  182. <mime:mimeXml part="Body" />
  183. </wsdl:output>
  184. </wsdl:operation>
  185. <wsdl:operation name="GetCitiesByCountry">
  186. <http:operation location="/GetCitiesByCountry" />
  187. <wsdl:input>
  188. <mime:content type="application/x-www-form-urlencoded" />
  189. </wsdl:input>
  190. <wsdl:output>
  191. <mime:mimeXml part="Body" />
  192. </wsdl:output>
  193. </wsdl:operation>
  194. </wsdl:binding>
  195. <wsdl:service name="GlobalWeather">
  196. <wsdl:port name="GlobalWeatherSoap" binding="tns:GlobalWeatherSoap">
  197. <soap:address location="http://www.webservicex.net/globalweather.asmx" />
  198. </wsdl:port>
  199. <wsdl:port name="GlobalWeatherSoap12" binding="tns:GlobalWeatherSoap12">
  200. <soap12:address location="http://www.webservicex.net/globalweather.asmx" />
  201. </wsdl:port>
  202. <wsdl:port name="GlobalWeatherHttpGet" binding="tns:GlobalWeatherHttpGet">
  203. <http:address location="http://www.webservicex.net/globalweather.asmx" />
  204. </wsdl:port>
  205. <wsdl:port name="GlobalWeatherHttpPost" binding="tns:GlobalWeatherHttpPost">
  206. <http:address location="http://www.webservicex.net/globalweather.asmx" />
  207. </wsdl:port>
  208. </wsdl:service>
  209. </wsdl:definitions>

另外github上还有一些Gradle的插件来帮你实现xml和java对象的转换,但是本质上其实还是使用了jaxb的xjc ant插件实现的,只不过隐藏了实现细节,使用起来更加方便。感兴趣的可以看https://github.com/jacobono/gradle-jaxb-plugin

在Gradle中使用jaxb的xjc插件的更多相关文章

  1. Gradle中使用idea插件的一些实践

    如果你的项目使用了Gradle作为构建工具,那么你一定要使用Gradle来自动生成IDE的项目文件,无需再手动的将源代码导入到你的IDE中去了. 如果你使用的是eclipse,可以在build.gra ...

  2. gradle中使用嵌入式(embedded) tomcat, debug 启动

    在gradle项目中使用embedded tomcat. 最开始部署项目需要手动将web项目打成war包,然后手动上传到tomcat的webapp下,然后启动tomcat来部署项目.这种手动工作通常还 ...

  3. Gradle中的buildScript代码块

    在编写Gradle脚本的时候,在build.gradle文件中经常看到这样的代码: build.gradle 1 2 3 4 5 6 7 8 9 buildScript { repositories ...

  4. AS Gradle构建工具与Android plugin插件【大全】

    Android plugin version 与 gradle version 的关系 Gradle是一种构建工具,它通过编写一个名为build.gradle的脚本文件对项目进行设置,再根据这个脚本对 ...

  5. [转] Gradle中的buildScript代码块

    PS: 在build script中的task apply plugin: 'spring-boot' 需要 classpath("org.springframework.boot:spri ...

  6. gradle中使用cobertura做代码覆盖(转)

    gradle很好用,但是默认是没有代码覆盖功能的,只好自己写.曾经在网上找到过别人的一段脚本,虽然也能用,但是有一些不爽的地方,一个原因是它不支持对层级工程中全部代码的覆盖,另一个原因是它用替换bui ...

  7. Gradle 1.12 翻译——第十七章. 从 Gradle 中调用 Ant

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  8. 在 Gradle 中使用 MyBatis Generator

    在 Intellij IDEA 中结合 Gradle 使用 MyBatis Generator 逆向生成代码 Info: JDK 1.8 Gradle 2.14 Intellij IDEA 2016. ...

  9. 如何在Maven和Gradle中配置使用Groovy 2.4与Spock 1.0

    如何在Maven和Gradle中配置使用Groovy 2.4与Spock 1.0 原文 https://dzone.com/articles/spock-10-groovy-24 翻译 hxfiref ...

随机推荐

  1. sublime配置react

    http://www.cnblogs.com/terrylin/p/4942332.html

  2. 深入研究C语言 第一篇(续)

    没有读过第一篇的读者,可以点击这里,阅读深入研究C语言的第一篇. 问题一:如何打印变量的地址? 我们用取地址符&,可以取到变量的偏移地址,用DS可以取到变量的段地址. 1.全局变量: 我们看到 ...

  3. BulkyCopy .Net

    It has being ages to get back to cnblogs, Career path had been changed back to .Net development in 4 ...

  4. .net 微信分享功能

    微信在国内目前无疑是最火的社交软件,智能手机装机必备. 微信api有java,php,Python语言的demo, 为毛没有C#的范例?兄长今天给各位带来一个.不叫哥(割)了,A股今天又暴跌[3912 ...

  5. sql连着function使用

    create function fun002(@thename varchar()) returns int as begin declare @count int select @count=cou ...

  6. 用eclipse搭建SSH(struts+spring+hibernate)框架

    声明: 本文是个人对ssh框架的学习.理解而编辑出来的,可能有不足之处,请大家谅解,但希望能帮助到大家,一起探讨,一起学习! Struts + Spring + Hibernate三者各自的特点都是什 ...

  7. [LeetCode]436 Find Right Interval

    Given a set of intervals, for each of the interval i, check if there exists an interval j whose star ...

  8. PHP内核探索:数组与链表

    在C语言中,我们可以自定义各种各样的数据结构,用来把很多数据保存在一个变量里面,但是每种数据结构都有自己的优缺点,PHP内核规模如此庞大,是否已经找到了一些非常棒的解决方法呢? 我们在选择各种数据结构 ...

  9. jQqery EasyUI dategrid行中多列数据的可编辑操作

    最近的项目中需要在前台dategrid列表中直接修改某些列的数据,并且修改后的数据需要不通过后台而自动更新在列表中. 带着这一问题开始寻找实现的思路,首先想到的就是去jQqery EasyUI官网找例 ...

  10. myeclipse使用maven插件进行maven install时报错check $m2_home environment variable and mvn script match

    check $m2_home environment variable and mvn script match 分类: maven2015-09-01 18:06 842人阅读 评论(0) 收藏 举 ...