Java XML JAXB Marshaller marshall(Object obj, URL schemaURL, Class... classesToBeBound)

Here you can find the source of marshall(Object obj, URL schemaURL, Class... classesToBeBound)

Description

marshall

License

Open Source License

Declaration

public static String marshall(Object obj, URL schemaURL, Class... classesToBeBound)
            throws JAXBException, SAXException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.xml.sax.SAXException;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import java.io.StringWriter;
import java.net.URL;

public class Main {
    public static String marshall(Object obj, URL schemaURL, Class... classesToBeBound)
            throws JAXBException, SAXException {

        JAXBContext context = JAXBContext.newInstance(classesToBeBound);

        Marshaller marshaller = context.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        if (schemaURL != null) {
            SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = sf.newSchema(schemaURL);
            marshaller.setSchema(schema);
        }/* ww  w . j  a v a2 s.c om*/

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);

        return writer.toString();
    }
}

Related

  1. marshalAsString(Class clz, T marshalObj)
  2. marshall(Class c, String xml)
  3. marshall(final Object o, Class clazz)
  4. marshall(Object o)
  5. marshall(Object obj)
  6. marshall(Object toMarshall)
  7. marshall(OutputStream os, JAXBElement element)
  8. marshall(String cntxtPkg, Object obj, OutputStream out)
  9. marshall(String file, JAXBElement object, Class context)