. You can think of JAXB as like a hobbled, wasteful serialization scheme that reads and writes XML (extensible Markup Language) files. The idea is its writes a custom Java progam for you that will then write or read the XML file. You can also think of it as a way of reading XML files, getting much of the bubblegum automatically generated for you. It is my preferred way to read XML, but it still half-assed (as is everything in XML). It takes your XSD (XML Scheme Definition) and constructs a getter and setter for each potential tag. The advantage is the getters and setters use Java types e.g. enum, int, String and XMLGregorianCalendar… not just String the way DOM (Document Object Model) does. Unfortunately, JAXB ignores the bounds specifications in your XSD schema. You must validate the XML document with your XSD separately.
To use it, compose and validate your XSD file describing your XML files. Then compile into Java source code like this:
Then study the generated code. That will make things clear fairly rapidly. The generated code considerately includes the relevant XSD sections as comments. It is mostly just garden-variety getters and setters.
You can get rid of the generated BigIntegers by defining your integer fields as type=xs:int rather than type=xs:postiveInteger in your XSD. This converts them to Integer, not int as you might expect. Why? Then null can be used for optional fields to indicate they were missing, even when there is a default value. Don’t sweat it. Autoboxing lets you treat the getters and setters as if they took ints.
JAXB thinks of itself as a sort of serialization. They refer to writing XML as marshalling and reading it is unmarshalling.
To write XML, you construct a tree of objects and then marshal them to an OutputStream. You start with an ObjectFactory, then create an object to represent the master enclosing tags, then create a JAXBElement. You create elements and glue them together with add. When you have the tree built, you create a JAXBContext and from that create a Marshaller, do a setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE ) finally marshal to an OutputStream.
Oddly, marshalling is much more complicated that unmarshalling. You normally expect reading XML to be considerably more complicated than writing it. It seems to me that writing XML directly with an OutputStream would normally be simpler and faster than using JAXB. I suspect the problem comes because the JAXB generated classes don’t contain the necessary machinery for writing.
What you need to do is a little simpler than the Unmarshalling tutorial suggests. You create a JAXBContext, then from that create an Unmarshaller. From that you can unmarshal an InputStream giving you a tree of objects you can chase.
This page is posted
Optional Replicator mirror
Your face IP:[126.96.36.199]
You are visitor number|