问题描述
我正在尝试在运行时将一堆XSD文件转换为Java源POJO.这些XML模式将基于某些协议特定的模型定义生成.
I'm trying to convert bunch of XSD files to Java source POJOs at runtime. These XML schemas will be generated based on some protocol specific model definitions.
我已经使用JAXB的XJC将架构编译为pojo.当我使用 Jaxb2-annotate-plugin 进行自定义注释时,开始出现问题.
I have used the JAXB's XJC to compile the schema to pojo. Started facing problem when I used Jaxb2-annotate-plugin for doing custom annotation.
为了进行测试,我在架构中添加了以下内容,
For testing, I have added following in my schema,
<xs:schema xmlns:aa="http://acme.example.com/system"
xmlns:another="http://acme.example.com/another"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:ncn="urn:ietf:params:xml:ns:netconf:notification:1.0"
jaxb:extensionBindingPrefixes="annox"
xmlns:annox="http://annox.dev.java.net"
targetNamespace="http://acme.example.com/system"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
XJC之后从那里开始出现以下错误,
XJC started giving the following error from there after,
根据各种来源提供的建议,在XJC的类路径中添加了必需的JARS,如下所示,但对我而言似乎没有什么锻炼的.
As per suggestions provided in various sources, added required JARS in XJC's classpath as follows, but nothing seems to workout for me.
我找不到使用带有jaxb2-annotate-plugin的XJC生成Java源代码的合适示例.我不能在我的maven/ant中添加它,正如我提到的那样,我必须在运行时生成xsd和pojo.
I couldn't find a proper example for generating java sources using XJC with jaxb2-annotate-plugin. I cannot add this in my maven/ant, as I mentioned I've to generate xsd and pojo at runtime.
如果将XJC工具用作Java进程是唯一的方法,请告诉我,
If using the XJC tool as java process is the only way, please let me know,
- 要在类路径中包括哪些从属jar?
- 如何激活插件和-Xannotate开关?
如果使用此插件可以通过其他方式生成pojo,请提出相同的建议.
If the pojo generation can be done in other ways with this plugin, please suggest the same.
我是jaxb的新手.任何建议都会非常有帮助.预先感谢!
I'm a newbie in jaxb. Any suggestions would be really helpful. Thanks in advance!
推荐答案
免责声明:我是 jaxb2-annotate-plugin .
首先,当xjc
与iff一起使用时,是指随JDK(如Windows下的xjc.exe
),XJC插件(包括 jaxb2-annotate-plugin )不适用于JDK中的xjc
二进制文件.
First, iff with xjc
you mean the binary xjc
distributed with JDK (like xjc.exe
under Windows), XJC plugins (including jaxb2-annotate-plugin) don't work with xjc
binary from JDK.
原因是,当为JDK打包XJC时,XJC包将从中重命名.com.sun.tools.xjc
至com.sun.tools.internal.xjc
.第三方XJC插件扩展了com.sun.tools.xjc.Plugin
而不是com.sun.tools.internal.xjc.Plugin
,这就是为什么它们不能与JDK打包的XJC一起使用的原因.
The reason is that when packaging XJC for JDK, the XJC packages get renamed from com.sun.tools.xjc
to com.sun.tools.internal.xjc
. Third-party XJC plugins extend com.sun.tools.xjc.Plugin
, not com.sun.tools.internal.xjc.Plugin
, this is why they don't work with JDK-packaged XJC.
接下来,您说您在运行时中生成代码.我不太了解这应该如何工作,您需要在以后编译类,等等.但是,好的,我假设您知道自己在做什么.无论如何,在这种情况下,您不应该通过命令行执行XJC.您可以通过com.sun.tools.xjc.api.XJC
使用编程API.检查此问题和答案:
Next, you say you generate code in the runtime. I don't quite understand how this is supposed to work, you'll need to compile the classes afterwards etc. But OK, I'll assume you know what you do. Anyway, you should not execute XJC via command line in this case. You have a programmatic API via com.sun.tools.xjc.api.XJC
. Check this question and answers:
- Add toString, hashCode, equals while generating JAXB classes in Java
- How do I add an xjc plugin programmatically at runtime?
相关:
这篇关于使用jaxb2-annotate-plugin和XJC工具进行自定义注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!