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.Marshaller;

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

    public static String parseObject2XmlString(Object object) {
        if (object == null) {
            return noResult;
        }
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContent;
        Marshaller marshaller;
        try {
            jAXBContent = JAXBContext.newInstance(object.getClass());
            marshaller = jAXBContent.createMarshaller();
            marshaller.marshal(object, sw);
            return sw.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return noResult;
        }
    }
}