XML Marshaling and Unmarshaling examples

The JiBX Schema Library artifacts can be used to manipulate xml.

Maven POM example

An excellent example of loading an xml file and doing some simple modifications can be found in the project building utility. Here is all you have to do:

Step 1: Add the schema's maven dependency to your project:


Step 2: Start writing code. As you can see, the code is pretty simple. After I open a inputStream to the POM file, I just unmarshal the document, make some changes, marshal it back to XML and write it.

IBindingFactory jc = BindingDirectory.getFactory(Model.class);
IUnmarshallingContext unmarshaller = jc.createUnmarshallingContext();
Model message = (Model)unmarshaller.unmarshalDocument( inStream, URL_ENCODING);


IBindingFactory jc = BindingDirectory.getFactory(Model.class);
IMarshallingContext marshaller = jc.createMarshallingContext();
marshaller.marshalDocument(message, URL_ENCODING, null, out);

You can find the complete source code for this example in the create-jibx-project-utilities maven project.

If you want a concrete example, get the jibx sample schema project from our repository. If you don't know how to download from a git tree, just grab the pom file and the java source file from the org.jibx.test.person sub-project. Change the version number of the dependencies to 1.1.4, and place them in a directory on your hard drive. Then type:

 mvn install

Take a look at the source code. It's pretty simple. It shows how to do a simple marshal and unmarshal operation.