Java XML JAXB Unmarshaller unmarshal(Class c, Object o)

Here you can find the source of unmarshal(Class c, Object o)

Description

unmarshal

License

Apache License

Declaration

@SuppressWarnings("unchecked")
    public static <T> String unmarshal(Class<T> c, Object o) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> String unmarshal(Class<T> c, Object o) throws Exception {

        JAXBContext ctx = JAXBContext.newInstance(c);
        Marshaller marshaller = ctx.createMarshaller();
        StringWriter entityXml = new StringWriter();
        marshaller.marshal(o, entityXml);

        String entityString = entityXml.toString();

        return entityString;
    }/*from w ww  . j av a  2 s.  c  om*/

    @SuppressWarnings("unchecked")
    public static <T> T marshal(Class<T> c, String xml) throws JAXBException {
        T res;

        if (c == xml.getClass()) {
            res = (T) xml;
        }

        else {
            JAXBContext ctx = JAXBContext.newInstance(c);
            Unmarshaller marshaller = ctx.createUnmarshaller();
            res = (T) marshaller.unmarshal(new StringReader(xml));
        }

        return res;
    }
}

Related

  1. getUnmarshaller()
  2. getUnmarshaller()
  3. getUnmarshaller(Class jaxbClass)
  4. marshallUnmarshall(T inObj)
  5. unmarshal(Class beanClass, InputStream is)
  6. unmarshal(Class clazz, Source source)
  7. unmarshal(Class clazz, String xml)
  8. unmarshal(Class clazz, String xmlInClassPath)
  9. unmarshal(Class clz, File file)