Java XML JAXB Unmarshaller unmarshal(String xml, Class clazz)

Here you can find the source of unmarshal(String xml, Class clazz)

Description

Parse an XML file into a container class.

License

Apache License

Parameter

Parameter Description
T the class type to be returned
xml the XML source content
clazz the parsed and populated class type; this is the same as the class type that is returned

Exception

Parameter Description
JAXBException if the XML source file does not match the input classtype

Return

the XML source file parsed into the identified class type

Declaration

public static <T> T unmarshal(String xml, Class<T> clazz)
        throws JAXBException 

Method Source Code

//package com.java2s;
/* /* w  w  w .  j a  v a2 s . co  m*/
 * Copyright 2015 Key Bridge LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.ByteArrayInputStream;

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

import javax.xml.bind.Unmarshaller;

public class Main {
    /**
     * Parse an XML file into a container class. This method calls the JAXB
     * un-marshaler and returns a class containing all of the content defined in
     * the XML file.
     * <p/>
     * @param <T>   the class type to be returned
     * @param xml   the XML source content
     * @param clazz the parsed and populated class type; this is the same as the
     *              class type that is returned
     * @return the XML source file parsed into the identified class type
     * @throws JAXBException if the XML source file does not match the input class
     *                       type
     */
    public static <T> T unmarshal(String xml, Class<T> clazz)
            throws JAXBException {
        Unmarshaller unmarshaller = JAXBContext.newInstance(clazz)
                .createUnmarshaller();
        return clazz.cast(unmarshaller.unmarshal(new ByteArrayInputStream(
                xml.getBytes())));
    }
}

Related

  1. unMarshal(String contextPath, InputStream xmlStream)
  2. unmarshal(String ObjXml, Class configurationClass)
  3. unmarshal(String packageName, InputStream inputStream)
  4. unmarshal(String string, Class clazz)
  5. unmarshal(String xml, Class c)
  6. unmarshal(String xml, Class clazz)
  7. unmarshal(T entity)
  8. unmarshalByContent(Class clazz, String xmlContent)
  9. unmarshalDOMElement(byte[] input)