Marshalling Java Classes to XML Elements

See Also 

The Java Architecture for XML Binding API (JAXB) provides a client application the ability to convert Java classes into a hierarchy of XML elements. The IDE provides a code template that you can use to generate a code snippet as the basis for this task.

To marshall Java classes to XML elements:

  1. Type jaxbm in the Source Editor for Java files and then press Tab. The snippet is created, as follows:
    try {    
        javax.xml.bind.JAXBContext jaxbCtx= javax.xml.bind.JAXBContext.newInstance( Object.class.getClass().getPackage().getName());
        javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(Object.class , System.out);
    } catch (javax.xml.bind.JAXBException ex) {
        // XXXTODO Handle exception
        java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
    }
  2. Now incorporate it in the rest of your code.
See Also
About Mapping XML to Java
Generating Java Classes from XML Schema Documents

Legal Notices