Women in Technology

Hear us Roar



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();




Full Threads Oldest First

Showing messages 1 through 5 of 5.

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

    Hi Deepak, I am working on JXB1.4 to create the XML document by binding the XML schema. Everything is fine except the Root element generation. My root element is not at all created. For marshalling, i am giving the root element object. But it didnt worked. what could be the reason/reasons???
    Hope you can help me to get rid of this problem.

    Thanks in advance.

    Gowtham J

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

      To marshall, we need to follow the following steps:
      1. Create a JAXBContext object and use this object to create a Marshaller object.
      2. Create an ObjectFactory object to create instances of relevant generated Java content classes.
      3. Using the ObjectFactory object, create an object tree with RootType as the root object. Populate these tree objects with relevant data using the appropriate setter methods.
      4. Create a JAXBElement<<i>RootType> object from CatalogType object. JAXBElement<<i>RootType> represents catalog element in XML document.

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

        Modify 4.

        4. Create a JAXBElement<RootType> object from RootType object. JAXBElement<RootType> represents root element in XML document.
  • JAXB 2.0
    2006-07-20 09:05:18  gpaladi [View]

    Hello,
    I tried all steps you have mentioned for JAXB 2.0. But when I run XJC, it's not creating all classes. I try to ran the sample catalog.xsd you have mentioned in the article. It created only the following 4 files.
    ArticleType
    CatalogType
    JournalType
    ObjectFactory

    Can you please help to fix this problem.

    Thanks
    Guru
    • JAXB 2.0
      2006-07-20 09:28:20  Deepak Vohra | [View]

      That's all the classes JAXB 2.0 creates, just the value classes, unlike JAXB 1.0 which creates interfaces and implementation classes, another advantage of JAXB 2.0.