Java XML JAXB Marshaller marshal(Class c, String xml)

Here you can find the source of marshal(Class c, String xml)

Description

marshal

License

Apache License

Declaration

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

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> T marshal(Class<T> c, String xml) throws JAXBException {
        T res;//w ww .java 2  s .  c o  m

        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;
    }

    @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;
    }
}

Related

  1. getSunNamespacePrefixMapper(@Nonnull final Marshaller aMarshaller)
  2. isInternalSunJAXB2Marshaller(@Nullable final Marshaller aMarshaller)
  3. isSunCanonicalization(@Nonnull final Marshaller aMarshaller)
  4. isSunJAXB2Marshaller(@Nullable final Marshaller aMarshaller)
  5. marshal(Class clz, T marshalObj)
  6. marshal(Class c, String xml)
  7. marshal(Class clazz, T obj)
  8. marshal(Class clazz, T object)
  9. marshal(final Object obj, final Class... classes)