Java XML JAXB Marshaller marshal(Marshaller marshaller, Object object, String filename)

Here you can find the source of marshal(Marshaller marshaller, Object object, String filename)

Description

marshal

License

Apache License

Declaration

public static void marshal(Marshaller marshaller, Object object, String filename)
        throws JAXBException, FileNotFoundException 

Method Source Code

//package com.java2s;
/**/*from www  .ja va  2s.co  m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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

import java.io.*;

public class Main {

    public static void marshal(Marshaller marshaller, Object object, String filename)
            throws JAXBException, FileNotFoundException {
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filename);
            marshaller.marshal(object, fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void marshal(Object object, String packageName, String filename, String schemaFile)
            throws JAXBException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.w3.org/2001/XMLSchema-instance");
        marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, schemaFile);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filename);
            marshaller.marshal(object, fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void marshal(Object object, String packageName, String filename)
            throws JAXBException, FileNotFoundException {
        JAXBContext jaxbContext = JAXBContext.newInstance(packageName);
        Marshaller marshaller = jaxbContext.createMarshaller();
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream(filename);
            marshaller.marshal(object, fileOutputStream);
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String marshal(String packageName, Object object) throws JAXBException, IOException {
        Marshaller marshaller = null;
        StringWriter writer = null;
        try {
            marshaller = createMarshaller(packageName);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            writer = new StringWriter();
            marshaller.marshal(object, writer);
            return writer.toString();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    public static String marshal(Marshaller marshaller, Object object) throws JAXBException, IOException {
        StringWriter writer = null;
        try {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            writer = new StringWriter();
            marshaller.marshal(object, writer);
            return writer.toString();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    public static Marshaller createMarshaller(String packageName) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(packageName);
        return context.createMarshaller();
    }
}

Related

  1. marshal(JAXBElement e, File f)
  2. marshal(JAXBElement jaxbElement, Class cls)
  3. marshal(JAXBElement value, String contextPath, OutputStream out, ClassLoader classLoader)
  4. marshal(Marshaller m, File out, Object o)
  5. marshal(Marshaller marshaller, Object object, String filename)
  6. marshal(Object bean)
  7. marshal(Object entity)
  8. marshal(Object obj)
  9. marshal(Object obj)