views:

152

answers:

2

The JAXB documentation is like a text-book, and I simply don't have to time to learn everything JAXB before I need to use it.

I have an XSD, if I want to use JAXB to marshal and un-marshal what is the workflow?

I don't need any specifics just a high level view.

What I know already: 1. JAXB can be used to take objects and create XML documents from them and vice versa 2. It uses some mysterious tool named "XJC" which I haven't been able to find a download of ANYWHERE on their website 3. XJC creates classes for you based on XSD

First, How do I find XJC? I actually know that it's installed on my system, not sure where I got it from though.
Second, whatever this tool is and how it got to my machine, how do I make it run with the most up to date version of JAXB? Third, so if my XSD changes do I really have to recreate the whole Java object, therefore possibly invalidating all my tests? Or can I define object to XSD mappings so that I'm in control of the mapping, not some default code generation?

I'm mostly used to taking XML and using something like xStream to manually unmarshal, but that's not an option for my purposes anymore.

A: 

Chances are you already have XJC installed. Check the bin directory of your JDK 6 install. Once found if you just run XJC it will give you the command line options.

JAXB is a spec, so there are multiple implementations:

If you modify your schema you will be able to tweak your object model, especially with the MOXy extensions.

To get started see: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted

Blaise Doughan
+4  A: 

Assuming you have JDK6;

xjc is in the bin/ folder of your JDK, something like C:\Program Files (x86)\Java\jdk1.6.0_20\bin\xjc.exe

The JDK comes with jaxb, while there might be newer versions around from https://jaxb.dev.java.net/ or other implementations available, you probably don't need to concern yourself with that at this point.

JAXB can do a lot of things, I'm not entierly sure exactly what you're trying to accomplish. If you have an xsd, you can generate java classes from it by running e.g.

xjc -p com.mypackage myschema.xsd

And include the generated classes in your project (More typically you'd run that as part of your build process).

Or can I define object to XSD mappings so that I'm in control of the mapping, not some default code generation?

The generated classes are just pojos with some annotations, you could create those classes yourself and be in full control of the mapping via annotations.

As said, jaxb can do a lot of things, here's just some basic examples, SearchParam/SearchType is a class generated by xjc from a small custom .xsd

Serialize an object to an XML file

JAXBContext context = JAXBContext.newInstance(SearchParam.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
List<String> hours = new LinkedList<String>();
hours.add("2009.12.11");
hours.add("2009.12.13");

SearchParam param = new SearchParam();
param.setFilter("greater");
param.setHours(hours);
param.setSearchType(SearchType.Fuzzy);
marshaller.marshal(param, new FileWriter("/tmp/SearchParam.xml"));    

Deserialize an xml file

 JAXBContext context = JAXBContext.newInstance(SearchParam.class);
 Unmarshaller unMarshaller = context.createUnmarshaller();
 SearchParam param = (SearchParam) unMarshaller.unmarshal(
                    new FileInputStream("/tmp/SearchParam.xml"));

Deserialize and do schema validation

JAXBContext context = JAXBContext.newInstance(SearchParam.class);
Unmarshaller unMarshaller = context.createUnmarshaller();
Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                 .newSchema(new  File("/tmp/schema1.xsd"));
unMarshaller.setSchema(schema);
SearchParam param = unMarshaller.unmarshal(
                      new FileInputStream("/tmp/SearchParam.xml"));

More information

nos
First, great answer, appreciated it. Question: So would you normally create the java objects and then just copy paste them into your project? You say that it would often be part of the build script, but that seems backwards to me, because then how will you know that the code you're writing that uses those objects will compile, much less run as expected? Plus, things like auto-complete couldn't work because the objects don't exist until you build the project.
walnutmon
Sure, "copy/pasting" the generated code is ok - the drawback is you you just gotta remember to do that when the schema changes. If you do it as part of your build, autocomplete will work fine, you just gotta run the build once first. Whether or not your project compiles or works as expected is no different from running xjc "by hand", and you might have unit test catching most of such errors.
nos