This discussion is archived
10 Replies Latest reply: Jan 4, 2012 10:28 PM by 872659 RSS

how to produce Java class from xsd programmatically?

872659 Newbie
Currently Being Moderated
As titled, I want to generage Java class/interface from xml schema via a program. There is a tool "xjc" that can do this manually.
I saw a sample that generates xsd file from Java class programmatically, I am figuring out if there is such a API to do the similar thing vice versa.

Thank you in advance!
  • 1. Re: how to produce Java class from xsd programmatically?
    jtahlborn Expert
    Currently Being Moderated
    "programmatically", meaning at runtime? or...? what exactly are you trying to accomplish?
  • 2. Re: how to produce Java class from xsd programmatically?
    872659 Newbie
    Currently Being Moderated
    Yes, I mean it at runtime.

    Like JAXBContext..generateSchema() that can generate XSD(XML Schema) from Java class at runtime, but I could not find such a similar API that can generate Java class from XSD.

    Thanks.
  • 3. Re: how to produce Java class from xsd programmatically?
    jtahlborn Expert
    Currently Being Moderated
    wasedaxiao wrote:
    Yes, I mean it at runtime.

    Like JAXBContext..generateSchema() that can generate XSD(XML Schema) from Java class at runtime, but I could not find such a similar API that can generate Java class from XSD.
    how would that be useful? since you don't generate the class until runtime, you can't really write any code to use the classes. so, what would be the point?
  • 4. Re: how to produce Java class from xsd programmatically?
    872659 Newbie
    Currently Being Moderated
    The tool xjc can generate Java class manually. Why there is no such API to do that programmatically?

    For example, someone needs to generate Java files from xsd files periodically that are updated sometimes. You can write a Java program to do that.
  • 5. Re: how to produce Java class from xsd programmatically?
    r035198x Pro
    Currently Being Moderated
    wasedaxiao wrote:
    The tool xjc can generate Java class manually. Why there is no such API to do that programmatically?

    For example, someone needs to generate Java files from xsd files periodically that are updated sometimes. You can write a Java program to do that.
    What's the usage scenario that allows for 'dynamic' changes in the XSD?
  • 6. Re: how to produce Java class from xsd programmatically?
    872659 Newbie
    Currently Being Moderated
    For now I don't have such a scenario that updates xsd files dynamically.

    Based on your questions, I suspect there is no such use case for generating Java file programmatically, that is why there is no such API provided, right?
  • 7. Re: how to produce Java class from xsd programmatically?
    r035198x Pro
    Currently Being Moderated
    wasedaxiao wrote:
    For now I don't have such a scenario that updates xsd files dynamically.

    Based on your questions, I suspect there is no such use case for generating Java file programmatically, that is why there is no such API provided, right?
    When you get a pojo with properties specified in an XSD then you generally want to be using that pojo's properties in your code. If the XSD that specs the pojo is changed, the code that uses the pojo usually has to change as well to start using the new properties or to stop using the now removed properties. In this case changing the XSD is always going to require other code changes anyway. Even if you do get the new class files, you now need to unload the previous versions of the pojo and somehow load the new version of the pojo.
    I'd rather not worry about these problems until a real requirement arises for it.
  • 8. Re: how to produce Java class from xsd programmatically?
    872659 Newbie
    Currently Being Moderated
    Thank you for your comments.

    Originally, I saw JAXBContext.generateSchema() can generate xsd from Java side and thought there should have counterpart vice versa.
  • 9. Re: how to produce Java class from xsd programmatically?
    rp0428 Guru
    Currently Being Moderated
    You may want to check out this Java EE 5 Tutorial: http://docs.oracle.com/javaee/5/tutorial/doc/bnbah.html

    It has an example of generating JAXB Java classes from an XML schema.

    Can you provide a sample xsd schema and the resulting Java class/classes you want to have produced?

    XMLBeans can use an xsd and generate Java classes for containing/manipulating XML objects, though I don't think that is what you are wanting.

    You can also write an XSLT stylesheet that generates Java classes based on parameters that you provide to the stylesheet but there is no xsd schema involved. We used this at a major national bank to produce Java classes for batch processing of files sent/received between the bank and third pary companies. The Java classes generated were used to read/write delimited files, xml files and fixed format files to/from Java bean classes with the necessary getters/setters for manipulating the data.

    Without a sample of what you are trying to doi it is hard to offer much help.
  • 10. Re: how to produce Java class from xsd programmatically?
    872659 Newbie
    Currently Being Moderated
    Thank you for the info.

    But I only see generating JAXB Java classes from an XML schema via xjc.
    Could you please point me which example that shows it through a test program?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points