Java XML JAXB String to Object convertStringToJAXB(String str)

Here you can find the source of convertStringToJAXB(String str)

Description

Converts a String object to a JAXB object.

License

Open Source License

Parameter

Parameter Description
str a <code>String</code> object

Return

a JAXB object converted from the String object.

Declaration

public static Object convertStringToJAXB(String str)
        throws JAXBException 

Method Source Code

//package com.java2s;
/**//from  w  w  w.java 2  s . c  o  m
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2007 Sun Microsystems Inc. All Rights Reserved
 *
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the License). You may not use this file except in
 * compliance with the License.
 *
 * You can obtain a copy of the License at
 * https://opensso.dev.java.net/public/CDDLv1.0.html or
 * opensso/legal/CDDLv1.0.txt
 * See the License for the specific language governing
 * permission and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * Header Notice in each file and include the License file
 * at opensso/legal/CDDLv1.0.txt.
 * If applicable, add the following below the CDDL Header,
 * with the fields enclosed by brackets [] replaced by
 * your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 *
 * $Id: WSFederationMetaUtils.java,v 1.5 2009-10-28 23:58:59 exu Exp $
 *
 */

import java.io.StringReader;

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

import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class Main {
    private static JAXBContext jaxbContext = null;

    /**
     * Converts a <code>String</code> object to a JAXB object.
     * @param str a <code>String</code> object
     * @return a JAXB object converted from the <code>String</code> object.
     * @exception JAXBException if an error occurs while converting
     *                          <code>String</code> object
     */
    public static Object convertStringToJAXB(String str)
            throws JAXBException {

        Unmarshaller u = jaxbContext.createUnmarshaller();
        return u.unmarshal(new StreamSource(new StringReader(str)));
    }
}

Related

  1. convertToJAXBException(String msg, Throwable e)
  2. convertToObject(Class clazz, InputStream inputStream)
  3. convertToXmlFile(File file, Object source, Class... type)
  4. convertXmlFileToObject(Class clazz, String xmlPath)