Java XML JAXB String to Object fromXml(Reader xml, Class clazz)

Here you can find the source of fromXml(Reader xml, Class clazz)

Description

Converts an input XML reader into the given type.

License

Open Source License

Parameter

Parameter Description
T the type to parse the XML into
xml the XML to convert
clazz the type to parse the XML into

Exception

Parameter Description
JAXBException XML Exception thrown if the conversion failed

Return

the xml data converted into the specified type

Declaration

@SuppressWarnings("unchecked")
public static synchronized <T> T fromXml(Reader xml, Class<T> clazz) throws JAXBException 

Method Source Code


//package com.java2s;
/*/*from  w  w w. j av  a2  s.  co m*/
 * This file is part of the ISIS IBEX application. Copyright (C) 2012-2016
 * Science & Technology Facilities Council. All rights reserved.
 *
 * This program is distributed in the hope that it will be useful. This program
 * and the accompanying materials are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution. EXCEPT AS
 * EXPRESSLY SET FORTH IN THE ECLIPSE PUBLIC LICENSE V1.0, THE PROGRAM AND
 * ACCOMPANYING MATERIALS ARE PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND. See the Eclipse Public License v1.0 for more
 * details.
 *
 * You should have received a copy of the Eclipse Public License v1.0 along with
 * this program; if not, you can obtain a copy from
 * https://www.eclipse.org/org/documents/epl-v10.php or
 * http://opensource.org/licenses/eclipse-1.0.php
 */

import java.io.Reader;
import java.io.StringReader;

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

import javax.xml.bind.Unmarshaller;

public class Main {
    /**
     * Converts an input XML reader into the given type.
     * 
     * @param <T>
     *            the type to parse the XML into
     * @param xml
     *            the XML to convert
     * @param clazz
     *            the type to parse the XML into
     * @return the xml data converted into the specified type
     * @throws JAXBException
     *             XML Exception thrown if the conversion failed
     */
    @SuppressWarnings("unchecked")
    public static synchronized <T> T fromXml(Reader xml, Class<T> clazz) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        return (T) unmarshaller.unmarshal(xml);
    }

    /**
     * Converts an input XML string into the given type.
     * 
     * @param <T>
     *            the type to parse the XML into
     * @param xml
     *            the XML to convert
     * @param clazz
     *            the type to parse the XML into
     * @return the xml data converted into the specified type
     * @throws JAXBException
     *             XML Exception thrown if the conversion failed
     */
    public static synchronized <T> T fromXml(String xml, Class<T> clazz) throws JAXBException {
        return fromXml(new StringReader(xml), clazz);
    }
}

Related

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