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.
JAXB provides a fast and convenient way to marshal (write) Java objects into XML and unmarshal (read) XML into objects. It supports a binding framework that maps XML elements and attributes to Java fields and properties using Java annotations.
In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema. JAXB can marshal XML data to XML documents, SAX content handlers, and DOM nodes.
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 http://jaxb.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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With