Java Architecture for XML Binding (JAXB)

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/

http://java.sun.com/developer/technicalArticles/WebServices/jaxb/xml_schema_fig1.gif

package rebecca.e.remote;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
 
public class XMLMarshal {
 
	public Object UnMurshall(String xml, String packageName)
			throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(packageName);
		Unmarshaller u = jc.createUnmarshaller();
		InputStream is = new ByteArrayInputStream(xml.getBytes());
		return u.unmarshal(is);
	}
 
	public String Murshall(Object xmlObject, String packageName)
			throws JAXBException {
 
		JAXBContext jc = JAXBContext.newInstance(packageName);
		Marshaller m = jc.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		ByteArrayOutputStream is = new ByteArrayOutputStream();
		m.marshal(xmlObject, is);
		return is.toString();
	}
 
}

Leave a comment

Your comment