One XSD, two code generation tools, two namespaces

不想你离开。 提交于 2019-12-20 07:16:44

问题


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

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.

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.

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?

Thank you

Edit: Since it seems to be that there is a missunderstanding, I added an example

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

  • C# Modelnamespace: com.foo.mySubNs.model (which fits the XSD namespace) all generated classes will have the same namespace provided though the MS codegen
  • Java Modelnamespace : com.foo.myOtherSubNs.model (which differs from the XSD namespace) the generated classes will have the "C# namespace". As a result the classes will not compile.

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.

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"));

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>

回答1:


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.




回答2:


You must take your pick as to what model is the main one: XSD, C#, or Java code. Once you make that choice, you must let the other two vary as they may. The best choice would be to make your XSD the reference model, generate the code in both languages with their respective tools, and just accept the results.

You can also try to pick the XML namespace such that the code at both ends will be satisfactory, but don't try to force anything to the last letter. That's not how it's meant to work.



来源:https://stackoverflow.com/questions/11503097/one-xsd-two-code-generation-tools-two-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!