Java XML JAXB Unserialize loadXMLFromString(Object object, String line)

Here you can find the source of loadXMLFromString(Object object, String line)

Description

load XML From String

License

Open Source License

Declaration

public static Object loadXMLFromString(Object object, String line) 

Method Source Code

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

import java.io.StringReader;

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

import javax.xml.bind.Unmarshaller;

public class Main {
    public static Object loadXMLFromString(Object object, String line) {
        Unmarshaller unmarshaller = createUnmarshaller(object);
        Object loaded = null;/*from   w  w  w . ja  v  a2 s. co  m*/
        StringReader stringReader = new StringReader(line);
        if (!line.isEmpty()) {
            try {
                loaded = unmarshaller.unmarshal(stringReader);
            } catch (JAXBException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return loaded;
    }

    @SuppressWarnings("rawtypes")
    private static Unmarshaller createUnmarshaller(Object object) {
        Class objectClass = object.getClass();

        if (objectClass.getSimpleName().equals("Class")) {
            objectClass = (Class) object;
        }

        JAXBContext context;
        Unmarshaller unmarshaller = null;
        try {
            context = JAXBContext.newInstance(objectClass);
            unmarshaller = context.createUnmarshaller();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return unmarshaller;
    }
}

Related

  1. loadObject(Class typeClass, URL path)
  2. loadObject(Path path, Class clazz)
  3. loadXML(Class type, File sourceFile)
  4. loadXml(File file, Class requireType)
  5. loadXML(Object object, String fileNamePath)
  6. parse(final Class clazz, final InputStream inputStream)
  7. parseIsoDate(String lexicalDate)
  8. parseJaxb(Class cls, InputStream in)
  9. parseXml(final Class klass, final Reader xmlReader)