Java examples for XML:JAXB
Translates String to Object.
/**//from w w w . j a v a 2 s . c o m * Copyright 2009 Welocalize, Inc. * * 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. * */ //package com.java2s; import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class Main { public static void main(String[] argv) throws Exception { Class clazz = String.class; String xml = "java2s.com"; System.out.println(string2Object(clazz, xml)); } /** * Translates String to Object. * @param ob * @return * @return */ public static <T> T string2Object(Class<T> clazz, String xml) { T ob = null; try { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller um = context.createUnmarshaller(); StringReader s = new StringReader(xml); ob = (T) um.unmarshal(s); } catch (JAXBException e) { e.printStackTrace(); } return ob; } }