Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.StringWriter;

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

public class Main {
    private static final String NO_RESULT = "<root>no result</root>";

    public static String objectToXml(Object object) {
        if (null == object) {
            return NO_RESULT;
        }

        StringWriter sw = new StringWriter();
        JAXBContext jAXBContent = null;
        Marshaller marshaller = null;

        try {
            jAXBContent = JAXBContext.newInstance(object.getClass());
            marshaller = jAXBContent.createMarshaller();
            marshaller.marshal(object, sw);

            return sw.toString();
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return NO_RESULT;
    }
}