tags:

views:

8413

answers:

8

I have a gigantic QuickBooks SDK .XSD schema file which defines XML requests/responses that I can send/receive from QuickBooks.

I'd like to be able to easily generate Java classes from these .XSD files, which I could then use to marshal XML to Java objects, and Java objects to XML.

Is there an easy way to do this...?

Ideally, it would not require any libraries external to the basic Java distro at run-time. But I'm flexible...

+6  A: 

XMLBeans will do it. Specifically the "scomp" command.

TofuBeer
+10  A: 

JAXB does EXACTLY what you want. It's built into the JRE/JDK starting at 1.6

basszero
+4  A: 

If you don't mind using an external library, I've used Castor to do this in the past.

Marc Novakowski
If you generate code with Castor, are those generated classes still dependent on Caster after the fact? Or could I move those classes to a machine without the Castor libraries, and they'd still work?
Keith Palmer
No, the generated classes are not dependent on the Castor libraries.
dave
Are there any good tutorials on how to use Castor to do this? It looks very promising... but Java is, to say the least, not my strongest language. I'm not sure what Castor files/packages I need to download and how to actually do the code generation... any step-by-step newbie examples around?
Keith Palmer
Check out this page for documentation on how to use the Castor SourceGenerator class: http://www.castor.org/sourcegen.html
Marc Novakowski
A: 

The well-known JAXB

There is a maven plugin that could do this for you at any build phase you want.

You could do this stuff in both ways: xsd <-> Java

François Wauquier
+3  A: 

If you want to start coding Java to XML and XML to Java in less than 5 minutes, try Simple XML Serialization. Don't spend hours learning the JAXB API http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php

However, if you are really keen on learning JAXB, here's an excellent tutorial http://blogs.sun.com/teera/entry/jaxb_for_simple_java_xml

Aries McRae
This looks *very* promising, and much simpler and nicer to work with for what I need it for than something big and complicated like JAXB. Unfortunately, I don't see any way to convert my existing .XSD to .class files, which is really what I need to start. Is there a way to do this?
Keith Palmer
A: 

Isn't JAXB's XJC is a possible answer to this? I'm trying to achieve the same thing. Still in the "trying" phase though. Came across XJC, so thought of sharing.

Nikhil Patil
+1  A: 

JAXB Limitation.

I worked on JAXB, as per my opinion its a nice way of dealing with data between XML and Java objects. The Positive sides are its proven and better in performance and control over the data during runtime. With a good usage of built tools or scripts it will takes away lot of coding efforts.

I found the configuration part is not a straight away task, and spent hours in getting the development environment setup.

However I dropped this solution due to a silly limitation I faced. My XML Schema Definition ( XSD ) has a attribute/element with name "value" and that I have to use XSD as it is. This very little constraint forced the my binding step XJC failed with a Error "Property 'Value' already used."

This is due to the JAXB implementation, the binding process tries to create Java objects out of XSD by adding few attributes to each class and one of them being a value attribute. When it processed my XSD it complained that there is already a property with that name.

RAm
A: 

Talking about JAXB limitation, a solution when having the same name for different attributes is adding inline jaxb customizations to the xsd:

+

. . binding declarations . .

or external customizations...

You can see further informations on : https://jaxb.dev.java.net/tutorial/section_5_3-Overriding-Names.html

Nady