ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Generating an XML Document with JAXB
Subject:   JAXB 2.0
Date:   2006-03-19 07:16:16
From:   dvohra09
For JAXB 2.0 the JAR files required and the Java classes generated are different.


1. JAXB 2.0 requires JDK 5.0, because JAXB 2.0 makes use of parameterized types.


2. For JAXB 2.0 the JAR files required are :


C:\Sun\jwsdp-2.0\jaxb\lib\jaxb-api.jar, C:\Sun\jwsdp-2.0\jaxb\lib\jaxb-impl.jar, C:\Sun\jwsdp-2.0\jaxb\lib\jaxb-xjc.jar, C:\Sun\jwsdp-2.0\jwsdp-shared\lib\activation.jar,
C:\Sun\jwsdp-2.0\sjsxp\lib\jsr173_api.jar, C:\Sun\jwsdp-2.0\sjsxp\lib\sjsxp.jar


3. For JAXB 2.0 a value class is generated for each top level complexType, instead of an interface and a implementation class. An ObjectFactory method is generated for each op level element.


4. For marshalling with JAXB 2.0, marshall with JAXBElement<CatalogType> object, instead of CatalogType object. CatalogType is value class corresponding to root element complexType catalogType.
//Create a CatalogType object
CatalogType catalog = factory.createCatalogType();
//Add elements and attributes to CatalogType object.
//Obtain a JAXBElement object from CatalogType object and marshall with the JAXBElement element.
JAXBElement<CatalogType> catalogElement=factory.createCatalog(catalog);
marshaller.marshal(catalogElement, new FileOutputStream(xmlDocument));


5. For unmarshalling with JAXB 2.0, unmarshalling returns a JAXBElement object.

JAXBContext jaxbContext = JAXBContext.newInstance("generated");
Unmarshaller unMarshaller = jaxbContext.createUnmarshaller();
JAXBElement<CatalogType> catalogElement = (JAXBElement<CatalogType>)
unMarshaller.unmarshal(xmlDocument);


//Obtain a CatalogType object from JAXBElement object.


CatalogType catalog=catalogElement.getValue();





1 to 2 of 2
  1. JAXB1.4 | Root element of the schema is not created
    2006-07-26 23:37:45  Gowtham_J [View]

    • JAXB1.4 | Root element of the schema is not created
      2006-07-27 11:30:10  Deepak Vohra | [View]

      • JAXB1.4 | Root element of the schema is not created
        2006-07-27 11:32:01  Deepak Vohra | [View]

  2. JAXB 2.0
    2006-07-20 09:05:18  gpaladi [View]

1 to 2 of 2