Java XML JAXB Unmarshaller unmarshall(String cntxtPkg, InputStream in)

Here you can find the source of unmarshall(String cntxtPkg, InputStream in)

Description

unmarshall

License

Open Source License

Declaration

public static Object unmarshall(String cntxtPkg, InputStream in)
            throws JAXBException, SAXException, IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.util.HashMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;
import org.xml.sax.SAXException;

public class Main {
    private static final HashMap<String, JAXBContext> marshallContexts = new HashMap<String, JAXBContext>();

    public static Object unmarshall(String cntxtPkg, InputStream in)
            throws JAXBException, SAXException, IOException {
        Unmarshaller unmarshaller = createUmarshall(cntxtPkg);
        if (unmarshaller == null)
            return null;
        return unmarshaller.unmarshal(in);
        // JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(in);
        ///*w w  w  .  j av a  2s .  c  om*/
        // return element.getValue();
    }

    public static Object unmarshall(String cntxtPkg, File xmlFile) throws JAXBException, SAXException, IOException {
        return unmarshall(cntxtPkg, new BufferedInputStream(new FileInputStream(xmlFile)));
    }

    public static Object unmarshall(String cntxtPkg, URL url) throws JAXBException, SAXException, IOException {
        InputStream is = url.openStream();
        return unmarshall(cntxtPkg, new BufferedInputStream(is));
    }

    public static Object unmarshall(String cntxtPkg, String filename)
            throws JAXBException, SAXException, IOException {
        return unmarshall(cntxtPkg, new BufferedInputStream(new FileInputStream(filename)));
    }

    private static Unmarshaller createUmarshall(String pkgName) throws JAXBException, SAXException {
        JAXBContext jaxbCtx = null;
        if ((jaxbCtx = marshallContexts.get(pkgName)) == null) {
            jaxbCtx = JAXBContext.newInstance(pkgName);
            marshallContexts.put(pkgName, jaxbCtx);
        }
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

        return unmarshaller;
    }
}

Related

  1. unmarshall(Class c, Object o)
  2. unmarshall(Class cls, Element domRequest)
  3. unmarshall(InputStream is, Class clz)
  4. unmarshall(InputStream toUnmarshall, Class clazz)
  5. unmarshall(JAXBContext c, Element e)
  6. unmarshall(String file, Class desiredClass, Class context)
  7. unmarshall(String xml, Class... classesToBeBound)
  8. unmarshall(String xml, Class clazz)
  9. unmarshall(String xml, URL schemaURL, Class... classesToBeBound)