Example usage for javax.xml.bind Unmarshaller unmarshal

List of usage examples for javax.xml.bind Unmarshaller unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller unmarshal.

Prototype

public <T> JAXBElement<T> unmarshal(javax.xml.stream.XMLEventReader reader, Class<T> declaredType)
        throws JAXBException;

Source Link

Document

Unmarshal root element to JAXB mapped declaredType and return the resulting content tree.

Usage

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(Home.class, Person.class, Animal.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource xml = new StreamSource("input.xml");
    Home home = unmarshaller.unmarshal(xml, Home.class).getValue();

    for (Object object : home.any) {
        System.out.println(object.getClass());
    }// w ww  .  j a  va  2  s .  c o  m
}

From source file:org.jasig.portlet.data.Importer.java

public static void main(String[] args) throws Exception {
    String dir = args[0];/*from   w  w w. j a  v  a  2s. c om*/
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);
    Object service = context.getBean(serviceBeanName);

    JAXBContext jc = JAXBContext.newInstance(modelClass);

    File folder = new File(dir);
    File[] files = folder.listFiles(new ImportFileFilter());

    for (File f : files) {
        StreamSource xml = new StreamSource(f.getAbsoluteFile());
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement je1 = unmarshaller.unmarshal(xml, modelClass);
        Object object = je1.getValue();
        Session session = sessionFactory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        Method method = service.getClass().getMethod(serviceBeanMethodName, modelClass);
        method.invoke(service, object);
        transaction.commit();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xmlString = "<message>HELLO!</message> ";
    JAXBContext jc = JAXBContext.newInstance(String.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StreamSource xmlSource = new StreamSource(new StringReader(xmlString));
    JAXBElement<String> je = unmarshaller.unmarshal(xmlSource, String.class);
    System.out.println(je.getValue());
}

From source file:MisspelledPersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();

    JAXBElement<Person> element = unmarshaller.unmarshal(new StreamSource("p.xml"), Person.class);
    Person person = element.getValue();/*w ww.  ja v a  2  s. c  o  m*/
    System.out.println(person.getFirstName());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Main.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    String code = "<book><title>Harry Potter</title></book>";
    StreamSource source = new StreamSource(new StringReader(code));
    JAXBElement<Main> jaxbElement = unmarshaller.unmarshal(source, Main.class);
}

From source file:Main.java

public static <T> T unMarshal(String content, Class<T> clazz) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    return unmarshaller.unmarshal(new StreamSource(new StringReader(content)), clazz).getValue();
}

From source file:Main.java

public static Object parseXmlFileToBeanByJAXB(String path, Class clase) throws Exception {

    JAXBContext context = JAXBContext.newInstance(clase);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement elm = unmarshaller.unmarshal(new StreamSource(new File(path)), clase);

    return elm.getValue();

}

From source file:se.inera.intyg.intygstjanst.web.integration.util.SendMessageToCareUtil.java

public static SendMessageToCareType getSendMessageToCareTypeFromFile(String fileName) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(SendMessageToCareType.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return unmarshaller.unmarshal(new StreamSource(new ClassPathResource(fileName).getInputStream()),
            SendMessageToCareType.class).getValue();
}

From source file:Main.java

public static <T> T converyToJavaBean(JAXBContext context, String xmlStr, Class<T> c) {
    JAXBElement<T> t = null;
    try {/*w w  w .j a  va  2  s .c  om*/
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (JAXBElement<T>) unmarshaller.unmarshal((new StreamSource(new StringReader(xmlStr))), c);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t.getValue();
}

From source file:Main.java

/**
 * Helper method to unmarshall a xml doc
 * @see http://docs.oracle.com/javase/tutorial/jaxb/intro/basic.html
 * http://jaxb.java.net/nonav/2.2.6/docs/ch03.html#unmarshalling
 * this uses <T> JAXBElement<T> unmarshal(Source source,
                    Class<T> declaredType)
                  throws JAXBException/*from  w  w  w.j a v  a2s .c o  m*/
 * 
 */
/*   public static <T> T unmarshall(Class<T> docClass, InputStream inputStream) throws JAXBException{
 String packageName = docClass.getPackage().getName();
 JAXBContext jc = JAXBContext.newInstance( packageName );
 Unmarshaller u = jc.createUnmarshaller();
 JAXBElement<T> root = u.unmarshal(new StreamSource(inputStream),docClass);
 return root.getValue();
 }*/

public static <T> T unmarshall(Class<T> docClass, InputStream inputStream)
        throws JAXBException, ParserConfigurationException, SAXException {
    String packageName = docClass.getPackage().getName();
    JAXBContext jc = JAXBContext.newInstance(packageName);
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/validation/schema", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    InputSource inputSource = new InputSource(inputStream);
    SAXSource source = new SAXSource(xmlReader, inputSource);

    Unmarshaller u = jc.createUnmarshaller();
    JAXBElement<T> root = u.unmarshal(source, docClass);
    return root.getValue();
}