Skip to main content
Jump to: navigation, search



The following example will demonstrate how to use EclipseLink's JAXB functionality to:

  • Generate JAXB-annotated Java classes from an XML Schema using the JAXB Compiler
  • Unmarshal an XML Document
  • Modify the XML data using the interfaces generated by the compiler
  • Marshal the modified data to XML


  1. Ensure that you have EclipseLink correctly installed and configured for your environment. Please see EclipseLink/Installing and Configuring EclipseLink for more information.
  2. Ensure that you have ANT correctly installed and configured.
  3. Unzip the Example ZIP file to a new directory.
  4. Edit the file in the root directory of the example and specify the path to your EclipseLink jlib directory:

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, arm, asm, asp, asymptote, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcl, dcpu16, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, freeswitch, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, haxe, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, ldif, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, nagios, netrexx, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, octave, oobas, oorexx, oracle11, oracle8, otj, oxygene, oz, parasail, parigp, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, pys60, python, q, qbasic, rails, rebol, reg, rexx, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, spark, sparql, sql, stonescript, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, upc, urbi, uscript, vala, vb, vbnet, vedit, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic

# Edit eclipselink.jlib to point to your EclipseLink jlib directory

You can compile and run the Example at any time by typing <tt>ant</tt> from the Example directory.  

==Running the JAXB Compiler==

The JAXB compiler can be run to generate JAXB-annotated Java classes from an XML Schema:

<ECLIPSELINK_HOME>/eclipselink/bin/ <source-file.xsd> [-options]
<ECLIPSELINK_HOME>\eclipselink\bin\jaxb-compiler.cmd <source-file.xsd> [-options]

Options include:
   -d <dir>                     Specifies the output directory for generated files
   -p <pkg>                     Specifies the target package
   -classpath <arg>             Specifies where to find user class files
   -verbose                     Enable verbose output
   -quiet                       Suppress compiler output
   -version                     Display version information

For a complete list of compiler options: -help

Example: -d jaxb-compiler-output config/Customer.xsd 

In this example, the JAXB Compiler is run from the "<tt>run.jaxb.compiler</tt>" task in the ANT build file.

==Creating a JAXBContext using

The standard way to specify which JAXB implementation should be used is through a file called <tt></tt>, which contains a single property, <tt>javax.xml.bind.context.factory</tt>. This file must be available on the classpath in the corresponding package (in this example, "<tt>org.example.customer_example</tt>").  To specify that the EclipseLink JAXB implementation should be used, your <tt></tt> file should have the following content:


In our example, we create the JAXBContext as follows:

JAXBContext jaxbContext = JAXBContext.newInstance("org.example.customer_example");

An alternate way to create a new JAXBContext is to supply an array of Class objects.  Again, the <tt></tt> file must be available on the classpath in the corresponding package:

Class[] classes = new Class[4]; 
classes[0] = org.example.customer_example.AddressType.class; 
classes[1] = org.example.customer_example.ContactInfo.class; 
classes[2] = org.example.customer_example.CustomerType.class; 
classes[3] = org.example.customer_example.PhoneNumber.class; 
JAXBContext jaxbContext = JAXBContext.newInstance(classes);

==Unmarshalling the XML Document==

With a JAXBContext created, we can now unmarshal an XML document using the statically generated classes:

File file = new File("Customer-data.xml");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StreamSource source = new StreamSource(file);
JAXBElement customerElement = unmarshaller.unmarshal(source, CustomerType.class);
CustomerType customer = (CustomerType) customerElement.getValue();

==Modify the Objects==

Below are examples of manipulating the XML data using the JAXB-generated static classes. Note that there are JavaBean-type accessors on the static interfaces.

Modifying the ShippingAddress ZipCode:


Adding a new PhoneNumber:

PhoneNumber homePhoneNumber  = new ObjectFactory().createPhoneNumber();
homePhoneNumber.setValue("(613) 555-3333");

Note the use of the <tt>ObjectFactory</tt> class to create a new <tt>PhoneNumber</tt> object.  In addition to generating the model classes used in this example, the JAXB compiler also generates an <tt>ObjectFactory</tt> class that can be used to create any of the generated types.

Removing all "cell" PhoneNumbers:

ArrayList phoneNumbersToRemove = new ArrayList();
List phoneNumbers = customer.getContactInfo().getPhoneNumber();
Iterator it = phoneNumbers.iterator();
while (it.hasNext()) {
    PhoneNumber phoneNumber = (PhoneNumber);
    if (phoneNumber.getType().equals("cell")) {



Back to the top