1 Reply Latest reply: Jun 20, 2008 1:57 AM by 807591 RSS

    Convert from Java to XSD

    Joshua
      How to convert from Java Object to XSD.

      I have used JAXB to convert a Java object to XML, but could not find any API which deals with conversion of
      Java objects to XSD.

      If there is any tool please let me know.

      Regards,
      Joshua.
        • 1. Re: Convert from Java to XSD
          807591
          There are tools around which generate XSDs from XML, so even if you can't find something to go from Java->XSD, you could use one of those in conjunction with, say, XStream. The problem with this is, though, that it's impossible for a tool to know exactly what the model should be from one example. For instance, there's no way to know what the cardinality of a particular property is by examining a class or object. Consider the following code:
          class SomeObject {
          
            public void setAddresses(List addresses) {//code}
            public List getAddresses() {}
            public void setJobCode(String jobCode) {}
            public String getJobCode() {}
          
          }
          Now, in the example above, how many addresses should an instance have? Is it at least one? Can it be zero? Can it be more than 5? There's no way to express that in Java. There is, however, a way to express it in an XML Schema. So you'll end up having to edit the schema by hand

          In the example above, what format does jobCode take? Suppose it should be an upper-case letter from A-H followed by three digits. Can you express that in Java? No. But you can in an XSD

          Now, in the example above, is the SomeObject type to be described in XML Schema as an element, or as a complex type? Again, there's no way to know, so you end up doing extra work by hand.

          So even if you manage to do this, you'd still have to hand-edit the XSD afterwards. In all honesty, I wouldn't bother. Writing XML Schema isn't all that hard, and a very useful skill. You're as well to write them yourself, that way you've got control over how it works, unless you absolutely don't care a hoot about the XML being validated against the schema, ever