Convert xml String To Object using JAXB - Java XML

Java examples for XML:JAXB

Description

Convert xml String To Object using JAXB

Demo Code


//package com.java2s;
import java.io.StringReader;

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

import javax.xml.bind.Unmarshaller;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        String xml = "java2s.com";
        System.out.println(xmlToObject(clazz, xml));
    }/*from   w  w  w.j av a 2s  .  com*/

    public static Object xmlToObject(Class clazz, String xml)
            throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller um = context.createUnmarshaller();
        return um.unmarshal(new StringReader(xml));
    }
}

Related Tutorials