Here you can find the source of unmarshal(String xml, Class
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 |
Parameter | Description |
---|---|
JAXBException | if the XML source file does not match the input classtype |
public static <T> T unmarshal(String xml, Class<T> clazz) throws JAXBException
//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()))); } }