Using the JAXB runtime to marshal and unmarshal XML documents

Use the Java™ Architecture for XML Binding (JAXB) run time to manipulate XML instance documents.

Before you begin

Use JAXB to generate Java classes from an XML schema with the schema compiler, xjc command or to generate an XML schema from a Java class with the schema generator, schemagen command.

About this task

Use JAXB APIs and tools to establish mappings between an XML schema and Java classes. After data bindings exist, use the JAXB binding runtime API to convert XML instance documents to and from Java objects. Data stored in an XML document is accessible without the need to understand the data structure. JAXB annotated classes and artifacts contains all the information that the JAXB runtime API needs to process XML instance documents. The JAXB runtime API enables marshaling of JAXB objects to XML and unmarshaling the XML document back to JAXB class instances.

Procedure

  • Marshal JAXB objects to XML instance documents.

    Use the JAXB runtime API to marshal or convert JAXB object instances into an XML instance document.

    1. Instantiate your JAXB classes.
    2. Invoke the JAXB marshaller.

    This example demonstrates how to instantiate the generated JAXB objects within an application and use the JAXBContext class and the JAXB runtime marshaller APIs to marshal the JAXB objects into XML instances.

    JAXBContext jc = JAXBContext.newInstance("myPackageName");
    //Create marshaller
    Marshaller m = jc.createMarshaller();
    //Marshal object into file.
    m.marshal(myJAXBObject, myOutputStream);
    The JAXB Reference Implementation introduces additional vendor specific marshaller properties such as namespace prefix mapping, indentation, and character escaping control that are not defined by the JAXB specification. Use these properties to specify additional controls of the marshaling process. These properties operate with the JAXB Reference Implementation only and might not with other JAXB providers. Additional information regarding the vendor specific properties is located in the Java Architecture for XML Binding JAXB RI Vendor Extensions Runtime Properties specification.
  • Unmarshal XML files to JAXB objects.

    Use the JAXB runtime API to unmarshal or convert an XML instance document to JAXB object instances.

    1. Obtain an existing XML instance document.
    2. Invoke the JAXB unmarshaller.

    This example demonstrates a program that reads an XML document and unmarshals or converts the XML document into JAXB object instances. Use the JAXBContext class and JAXB runtime Unmarshaller APIs to unmarshal the XML document.

    JAXBContext jc = JAXBContext.newInstance("myPackageName");
    //Create unmarshaller
    Unmarshaller um = jc.createUnmarshaller();
    //Unmarshal XML contents of the file myDoc.xml into your Java 
      object instance.
    MyJAXBObject myJAXBObject = (MyJAXBObject) 
    um.unmarshal(new java.io.FileInputStream( "myDoc.xml" ));

Results

You can now marshal JAXB Java classes, and unmarshal XML data using the JAXB binding framework. Refer to the JAXB Reference implementation documentation for additional information about the marshal and unmarshal runtime APIs

Avoid trouble: If Java 2 Security is enabled, wrap your JAXBContext.newInstance(), Unmarshaller.unmarshal() and, Marshaller.marshal() method calls within a AccessController.doPrivileged method to avoid a security exception.
.