RuntimeAfter running the binding compiler the modified classes are ready to be used.
You'll need to include the /lib/jibx-run.jar and
/lib/xpp3.jar jar files from the distribution in your application
classpath (but not the /lib/bcel.jar and /lib/jibx-bind.jar
files, which are only used by the binding compiler). You'll also need to add a
little code at whatever point you want to marshal or unmarshal a document. This uses the
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class); Here Customer is the name of a class with a global mapping in
the binding. The IUnmarshallingContext uctx = bfact.createUnmarshallingContext(); Object obj = uctx.unmarshalDocument (new FileInputStream("filename.xml"), null); This is just one of several variations of an unmarshal call, in this case to unmarshal an XML document in the file filename.xml. You can pass a reader instead of a stream as the source of the document data if you want, and can also specify an encoding for the document - see the JavaDocs for details. The returned object will be an instance of one of your classes defined with a global mapping in the binding - you can either check the type with instanceof or cast directly to your object type, if you know what it is. Marshalling is just as easy. Here's an example: IMarshallingContext mctx = bfact.createMarshallingContext(); mctx.marshalDocument(obj, "UTF-8", null, new FileOutputStream("filename.xml")); As with the unmarshal example, this is just one of several variations that
can be used for the marshal call. This marshals the object to an XML document
written to the file filename.xml, with UTF-8 character encoding
(the most common choice for XML). The code as shown writes the output document
with no extra whitespace; you can use the Character encodingsThe Java core classes provides Because of this need to use character references, JiBX supports the use of
character escapers for output conversion handling. For the widely-used
UTF-8 and ISO-8859-1 (Western European character set) encodings implementations
are included that handle both stream and writer output formats automatically
(though using a stream will provide the best performance). The US-ASCII 7-bit
format is also handled automatically, though in this case a
Other character encodings can be used for output if you supply an appropriate
Output formatsJiBX also provides the ability to generate output formats other than text, by
using different implementations of the StAX supportThe 1.1 release adds support for StAX parser input and StAX writer output. To select which parser you want to use dynamically, set the system property org.jibx.runtime.impl.parser to the value org.jibx.runtime.impl.XMLPullReaderFactory to select the XPP3 XMLPull parser, or the value org.jibx.runtime.impl.StAXReaderFactory to select the StAX parser. By default, JiBX uses whichever parser implementation it finds at runtime, with preference given to the XPP3 XMLPull parser if both XPP3 and a StAX parser are present. If you never want to use the XPP3 parser, simply remove the /lib/xpp3.jar file from your JiBX installation. Some StAX parsers support schema validation of input. If you wish to make use of this feature you'll need to substitute an appropriate StAX parser implementation for the /lib/wstx-asl.jar StAX parser included in the JiBX distribution, and take whatever action is needed to enable schema validation. JiBX 1.1 also includes support for StAX output (using the
// marshal root object back out to document in memory IMarshallingContext mctx = bfact.createMarshallingContext(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { XMLOutputFactory ofact = XMLOutputFactory.newInstance(); XMLStreamWriter wrtr = ofact.createXMLStreamWriter(bos, enc); mctx.setXmlWriter(new StAXWriter(bfact.getNamespaces(), wrtr)); mctx.marshalDocument(obj); } catch (XMLStreamException e) { throw new JiBXException("Error creating writer", e); } |