Skip to main content

[Nubie] Where is the schema?

4 replies [Last post]
ferr0084
Offline
Joined: 2005-06-24

I'm having problems understanding some JAXB basics.

I'm try to use annotations to convert a java object into xml. This has "worked" in a very simplistic way but I now need to have more control over what the xml looks like. I'm getting confused by all the documentation that references xsd files and schema generated classes. I have no xsd files. I have no schema generated classes. I just have annotated java. Where is the schema?

Perhaps a more important question is - am I the only one doing things this way? I would think that going from java to xml would be the simplest and most common use case yet I have found almost no documentation on this. What am I missing??

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09

Yep. Going from java to XML is quite simple.

What kind of documentation are you trying to find? is it "How to control XML representation by using annotation"? Have you looked at https://jaxb.dev.java.net/guide/ ?

ferr0084
Offline
Joined: 2005-06-24

Yes, something like that would be great! I did look at your unofficial guide but it didn't have the info I'm looking for.

Sorry but I'm still confused about the schema. Does it get generated and stay in memory? Should I be using a schema instead of relying on annotations? Do annotations provide the same flexibility and power as the schema? In general, what are the drawbacks/benefits of doing it either way?

The specific problem I'm trying to solve is regarding grouping of elements. I need to group several fields together in the xml but they are not part of a collection. For example, I've annotated a Java class to generate this xml...



ALLXPO_0222-0705
8800989447
0
TETO DF 1
Allegro
1
0%

0.0

Proforma
1/9/2006
NIST000359
1/10/2006

I would like to be able to group the elements together within a tag but all the documentation I've seen regarding grouping deals with collections, not a set of individual properties. I'd rather not create a new class to contain them if I can avoid it. How would you solve this problem?

Thanks for your help!

kohsuke
Offline
Joined: 2003-06-09

I'm sorry, but your questions aren't making much sense to me.

When going from Java to schema, annotations are what you work with. You put those, and they affect the XML representation. JAXB can generate a concrete schema file for you from annotations if you want (just like you can generate class files from java files if you want --- but you wouldn't ask "should I be using class files instead of java files? Do java files provide the same flexibility and power of the class files?".)

As for your later question, doing so requires that you split that fleetInfo portion to its own class.

ferr0084
Offline
Joined: 2005-06-24

Now we're getting somewhere.

You answered my question - I can't do the grouping that I want to do with annotations alone. I need to restructure my classes. That sucks. Oh well.

I think I smell what you're saying about the schema, thanks!