Java XML JAXB String to Object fromXml(String responseBody, Class c)

Here you can find the source of fromXml(String responseBody, Class c)

Description

from Xml

License

Open Source License

Declaration

public static <T> T fromXml(String responseBody, Class<T> c) throws JAXBException, XMLStreamException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import java.io.ByteArrayInputStream;

public class Main {
    private static XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

    public static <T> T fromXml(String responseBody, Class<T> c) throws JAXBException, XMLStreamException {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody.getBytes());
        JAXBContext jaxbContext = JAXBContext.newInstance(c);
        XMLStreamReader xsr = xmlInputFactory.createXMLStreamReader(inputStream);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (T) jaxbUnmarshaller.unmarshal(xsr);
    }//  w w w . j  a v  a2  s  .co  m
}

Related

  1. fromXML(final byte[] data, final Class type)
  2. fromXml(final Reader reader, final Class dtoClass)
  3. fromXml(InputStream input, Class clazz)
  4. fromXml(InputStream xml, Class objectClass)
  5. fromXml(Reader xml, Class clazz)
  6. fromXml(String xml, Class clazz)
  7. fromXml(String xml, Class type)
  8. fromXML(String xml, Class valueType)
  9. getXmlObject(String xml, Class cls)