Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.*;

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

    public static String parseObjToXmlString(Object obj) {
        if (obj == null) {
            return noResult;
        }
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContext;
        Marshaller marshaller;
        try {
            jAXBContext = JAXBContext.newInstance(obj.getClass());
            marshaller = jAXBContext.createMarshaller();
            marshaller.marshal(obj, sw);
            return sw.toString();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return noResult;
    }
}