Sunday, October 2, 2011

JABX2..cool.. Sample example

Steps. Download Binary

xjc exampe\IpopsOperation.xsd -p com.xmlpojo

xjc XSD_location -p Create_pojos_at_package


Simple Main Java

package com.main;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.StringReader;
import java.math.BigDecimal;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import nz.co.equinox.training.domain.booking.*;

public class TestJaxb2 {

/**
* @param args
*/

public static void UmMarshal() {
try {
JAXBContext jaxbContext = JAXBContext
.newInstance("nz.co.equinox.training.domain.booking");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement bookingElement = (JAXBElement) unmarshaller
.unmarshal(new File("C:/Harish/Technical/JAXB2/jaxb-ri-20110512/bin/exampe/Data.xml"));



/*StringBuffer xmlStr = new StringBuffer( "" );
JAXBElement bookingElement = (JAXBElement) unmarshaller.unmarshal(new StreamSource( new StringReader( xmlStr.toString() ) ));*/



CourseBooking booking = bookingElement.getValue();
System.out.println(booking.getCourseReference());
System.out.println(booking.getInvoiceReference());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void marshal() {

try {

JAXBContext jaxbContext = JAXBContext
.newInstance("nz.co.equinox.training.domain.booking");

CourseBooking booking = new CourseBooking();
booking.setCourseReference("UML-101");
booking.setTotalPrice(new BigDecimal(10000));
booking.setInvoiceReference("123456");
DatatypeFactory datatypes;

datatypes = DatatypeFactory.newInstance();

booking.setCourseDate(datatypes.newXMLGregorianCalendarDate(2006,
06, 15, 0));
booking.setTotalPrice(new BigDecimal(10000));
booking.setInvoiceReference("123456");
booking.getStudent().add(new StudentType());
booking.getStudent().get(0).setFirstName("John");
booking.getStudent().get(0).setSurname("Smith");
booking.setCompany(new CompanyType());
booking.getCompany().setName("Clients inc");
booking.getCompany().setContact(new ContactType());
booking.getCompany().getContact().setName("Paul");
booking.getCompany().getContact().setEmail("paul@clients.inc");
booking.getCompany().getContact().setTelephone("12345678");
booking.getCompany().setAddress("10 client street");


// Marshal to System.out
Marshaller marshaller = jaxbContext.createMarshaller();
JAXBElement bookingElement = (new ObjectFactory())
.createBooking(booking);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);

//StreamResult result = new StreamResult( System.out );

//System.out.println(result.getOutputStream());

ByteArrayOutputStream byteara = new ByteArrayOutputStream();
marshaller.marshal(bookingElement, byteara);
System.out.println("1:"+byteara.toString());

/*DOMResult result = new DOMResult();
marshaller.marshal( bookingElement, result );*/


} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
marshal();
//UmMarshal();
}

}
------------------------------------------------------------------------

Simple Main

package com.main;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;


import com.xmlpojo.*;



public class IPOPSTest {

public static void UmMarshal() {
try {
//File f = new File();
InputStream is = new FileInputStream( "C:/Harish/Technical/JAXB2/jaxb-ri-20110512/bin/exampe/OrderResponse.xml" );
JAXBContext jaxbContext = JAXBContext.newInstance(IPOPSOperation.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object o = unmarshaller.unmarshal(is);
if(o instanceof IPOPSOperation){
//System.out.println("1");
IPOPSOperation operation = (IPOPSOperation) o;
MessageBodyType messageBodyType = operation.getMessageBody();
BusinessInputType businessInputType = messageBodyType.getBusinessInput();
List list = businessInputType.getAPIDETAILS();
System.out.println("----------------businessInput--------------");
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
ApiDetailsType apiDetailsType = (ApiDetailsType) iterator.next();

System.out.println(apiDetailsType.getARGNAME()+":"+apiDetailsType.getARGVALUE());

}
System.out.println("----------------businessOutputData--------------");
BusinessOutputType businessOutputType = messageBodyType.getBusinessOutputData();
List list2 = businessOutputType.getOutputTypes();
for (Iterator iterator = list2.iterator(); iterator.hasNext();) {
OutputType outputType = (OutputType) iterator.next();
System.out.println(outputType.getName()+":"+outputType.getValue());


}
System.out.println("----------------getOutputTypeList--------------");
List list3 = businessOutputType.getOutputTypeList();
for (Iterator iterator = list3.iterator(); iterator.hasNext();) {
OutputTypes outputTypes = (OutputTypes) iterator.next();
List list4 =outputTypes.getOutputTypes();
for (Iterator iterator2 = list4.iterator(); iterator2
.hasNext();) {
OutputType outputType = (OutputType) iterator2.next();
System.out.println(outputType.getName()+":"+outputType.getValue());


}
System.out.println("----------------Type Line Items--------------");
}

}



/*StringBuffer xmlStr = new StringBuffer( "" );
JAXBElement bookingElement = (JAXBElement) unmarshaller.unmarshal(new StreamSource( new StringReader( xmlStr.toString() ) ));*/




//System.out.println(bookingElement.getValue());
//System.out.println(booking.getInvoiceReference());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void marshal() {

try {

JAXBContext jaxbContext = JAXBContext
.newInstance("com.xmlpojo");






// Marshal to System.out
Marshaller marshaller = jaxbContext.createMarshaller();
JAXBElement bookingElement =null ;// (new ObjectFactory()).createIPOPSOperation();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
Boolean.TRUE);

//StreamResult result = new StreamResult( System.out );

//System.out.println(result.getOutputStream());

ByteArrayOutputStream byteara = new ByteArrayOutputStream();
marshaller.marshal(bookingElement, byteara);
System.out.println(byteara.toString());

/*DOMResult result = new DOMResult();
marshaller.marshal( bookingElement, result );*/


} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args) {
//marshal();
UmMarshal();
}


}
---------------------------------------------------------------------------------

Followers