一个XSD,两个代码生成工具,两个名称空间 [英] One XSD, two code generation tools, two namespaces

查看:218
本文介绍了一个XSD,两个代码生成工具,两个名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XSD文件,我也想从中生成C#和Java类。

I have a XSD file, from which I want to generate C# and Java classes as well.

我首先根据我的C#名称空间在XSD中设置名称空间我的班级所在的地方。一代(使用Microsoft工具)工作正常,序列化工作也很好,我可以针对XSD验证它们 - 完美。

I first set the namespace in the XSD according to my C# namespace where my classes resides. The generation (with the Microsoft tools) works fine and also the serialisation works great and I can validat them against the XSD - perfect.

现在我想创建一个java类JAXB。
问题是要创建的类具有与C#中的类不同的包结构。因此,当我将XSD命名空间设置为java的包结构时,它工作正常。我可以序列化并验证XML。

Now I want to create java classes with JAXB. The problem is that the classes which are going to be created have a different package structure then the one in C#. So when I set the XSD namespace to the package structure of java, it works fine. I can serialize and validate the XML.

现在我的问题:
有没有办法解决这个问题? (两个生成工具都有一个XSD)
我是否对实际需要的名称空间缺乏了解?

Now my question(s): Is there a way to solve this? (Have one XSD for both generation tools) Do I lack a understanding of what the namespace actually is needed for?

谢谢

编辑:由于似乎存在误解,我添加了一个示例

XSD:targetNamespace =http://foo.bar/mySubNs/model


  • C#Modelnamespace:com.foo .mySubNs.model(适合XSD命名空间)
    所有生成的类都将具有相同的命名空间,尽管MS codegen

  • Java Modelnamespace:com.foo.myOtherSubNs.model(与XSD名称空间不同)
    生成的类将具有C#名称空间。因此,类将无法编译。

如果我在java代码生成期间更改命名空间,我可以编译类。到现在为止还挺好。但是我无法通过针对XSD的java类验证生成的XML,因为命名空间不同。

If I would change the namespace during the code generation for java, I can compile the classes. So far so good. But I won't be able to validate the generated XML by that java classes against the XSD, since the namespace differs.

要用Java编组我的对象,我使用JAXB像这样:

To marshall my objects in Java, I use JAXB like this:

  ValidationEventCollector validationCollector = new ValidationEventCollector();
  SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
  Schema schema = schemaFactory.newSchema(new File ("my/schema/location"));

  // JAXB_CONTEXT is just an instance of "JAXBContext"
  Marshaller marshaller = JAXB_CONTEXT.createMarshaller();    

  marshaller.setSchema(schema);

  marshaller.setEventHandler(validationCollector);

  marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

  JAXBElement<MyClass> root = new JAXBElement<MyClass> (  new QName(MyClass.class.getPackage().getName(),"MyClass"),MyClass.class, node);

  marshaller.marshal(root, new File("output/Path/obj.xml"));

要从模式构建我的java类,我在ant构建脚本中使用xjc-task: / p>

To build my java classes from the schema I use a xjc-task in an ant build script:

<xjc destdir="${dir.src.gen}" removeOldOutput="no" extension="true">
    <schema dir="${dir.schema}" includes="${file.schema}"/>
    <binding dir="${dir.schema}" includes="*.xjb"/>
    <produces dir="${dir.src.gen}" includes="**/*.java"/>
</xjc>


推荐答案

XSD命名空间不必与包匹配结构,至少不是Java。使用JAXB生成类时,只需提供要将类放入的包。

The XSD namespace doesn't have to match the package structure, at least not in Java. When generating the classes using JAXB just provide the package you want to put the classes into.

这篇关于一个XSD,两个代码生成工具,两个名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆