Basic Examples

This section describes the Basic examples (Modify Marshal, Unmarshal Validate) that demonstrate how to:

Modify Marshal Example

The purpose of the Modify Marshal example is to demonstrate how to modify a Java content tree.

  1. The <INSTALL>/examples/jaxb/modify-marshal/
    Main.java
    class declares imports for three standard Java classes plus four JAXB binding framework classes and primer.po package:
  2. import java.io.FileInputStream;
    import java.io.IOException;
    import java.math.BigDecimal;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    import primer.po.*;

  3. A JAXBContext instance is created for handling classes generated in primer.po.
  4. JAXBContext jc = JAXBContext.newInstance( "primer.po" );

  5. An Unmarshaller instance is created, and po.xml is unmarshalled.
  6. Unmarshaller u = jc.createUnmarshaller();
    PurchaseOrder po =
      (PurchaseOrder)u.unmarshal(
        new FileInputStream( "po.xml" ) );

  7. set methods are used to modify information in the address branch of the content tree.
  8. USAddress address = po.getBillTo();
    address.setName( "John Bob" );
    address.setStreet( "242 Main Street" );
    address.setCity( "Beverly Hills" );
    address.setState( "CA" );
    address.setZip( new BigDecimal( "90210" ) );

  9. A Marshaller instance is created, and the updated XML content is marshalled to system.out. The setProperty API is used to specify output encoding; in this case formatted (human readable) XML format.
  10. Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
      Boolean.TRUE);
    m.marshal( po, System.out );

Sample Output

Running java Main for this example produces the following output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<purchaseOrder orderDate="1999-10-20-05:00">
<shipTo country="US">
<name>Alice Smith</name>
<street>123 Maple Street</street>
<city>Cambridge</city>
<state>MA</state>
<zip>12345</zip>
</shipTo>
<billTo country="US">
<name>John Bob</name>
<street>242 Main Street</street>
<city>Beverly Hills</city>
<state>CA</state>
<zip>90210</zip>
</billTo>
<items>
<item partNum="242-NO">
<productName>Nosferatu - Special Edition (1929)</productName>
<quantity>5</quantity>
<USPrice>19.99</USPrice>
</item>
<item partNum="242-MU">
<productName>The Mummy (1959)</productName>
<quantity>3</quantity>
<USPrice>19.98</USPrice>
</item>
<item partNum="242-GZ">
<productName>
Godzilla and Mothra: Battle for Earth/Godzilla vs. King Ghidora
</productName>
<quantity>3</quantity>
<USPrice>27.95</USPrice>
</item>
</items>
</purchaseOrder> 

Unmarshal Validate Example

The Unmarshal Validate example demonstrates how to enable validation during unmarshalling (Unmarshal-Time Validation). Note that JAXB provides functions for validation during unmarshalling but not during marshalling. Validation is explained in more detail in More About Validation.

  1. The <INSTALL>/examples/jaxb/unmarshal-validate/Main.java
    class declares imports for three standard Java classes plus seven JAXB binding framework classes and the primer.po package:
  2. import java.io.FileInputStream;
    import java.io.IOException;
    import java.math.BigDecimal;
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.UnmarshalException;
    import javax.xml.bind.Unmarshaller;
    import javax.xml.bind.ValidationEvent;
    import javax.xml.bind.util.ValidationEventCollector;
    import primer.po.*;

  3. A JAXBContext instance is created for handling classes generated in primer.po.
  4. JAXBContext jc = JAXBContext.newInstance( "primer.po" );

  5. An Unmarshaller instance is created.
  6. Unmarshaller u = jc.createUnmarshaller();

  7. The default JAXB Unmarshaller ValidationEventHandler is enabled to send to validation warnings and errors to system.out. The default configuration causes the unmarshal operation to fail upon encountering the first validation error.
  8. u.setValidating( true );

  9. An attempt is made to unmarshal po.xml into a Java content tree. For the purposes of this example, the po.xml contains a deliberate error.
  10. PurchaseOrder po =
      (PurchaseOrder)u.unmarshal(
        new FileInputStream("po.xml"));

  11. The default validation event handler processes a validation error, generates output to system.out, and then an exception is thrown.
  12. } catch( UnmarshalException ue ) {
      System.out.println( "Caught UnmarshalException" );
    } catch( JAXBException je ) {
      je.printStackTrace();
    } catch( IOException ioe ) {
      ioe.printStackTrace();

Sample Output

Running java Main for this example produces the following output:

DefaultValidationEventHandler: [ERROR]: "-1" does not satisfy the "positiveInteger" type
Caught UnmarshalException