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 Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;

Source Link

Document

Unmarshal XML data from the specified pull parser and return the resulting content tree.

Usage

From source file:com.testdroid.api.APIEntity.java

@JsonIgnore
public static <T extends APIEntity> T fromXML(String xml, Class<T> type) {
    try {/*from www  .jav a2 s  . c o  m*/
        JAXBContext context = getJAXBContext(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException ex) {
        Logger.getLogger(APIEntity.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:Main.java

public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
    Object xmlObject = null;/*from   w w  w.j  a  va2s .  c o m*/
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        FileReader fr = null;
        try {
            fr = new FileReader(xmlPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        xmlObject = unmarshaller.unmarshal(fr);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return xmlObject;
}

From source file:gov.nih.nci.cabig.caaers.web.search.AdvancedSearchUiUtil.java

/**
 * Will parse the AdvancedSearchUi// w w  w  . j a  v a 2s. c  om
 * @return
 */

public static AdvancedSearchUi loadAdvancedSearchUi() throws CaaersSystemException {

    AdvancedSearchUi advancedSearchUi = null;

    InputStream inputStream = Thread.currentThread().getContextClassLoader()
            .getResourceAsStream("advancedSearch-ui.xml");
    Unmarshaller unmarshaller;
    try {
        unmarshaller = JAXBContext.newInstance("gov.nih.nci.cabig.caaers.web.search.ui").createUnmarshaller();
        advancedSearchUi = (AdvancedSearchUi) unmarshaller.unmarshal(inputStream);
    } catch (Exception e) {
        log.error("Error while loading advanced search ui.", e);
        throw new CaaersSystemException("Unable to parse advancedSearch-ui.xml", e);
    }

    return advancedSearchUi;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Object convertXmlPathToObject(String xmlPath, Class clazz) {
    Object object = null;//from  w  w  w  .  j  ava2 s.c  om
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        FileReader xmlReader = null;
        try {
            xmlReader = new FileReader(xmlPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        object = unmarshaller.unmarshal(xmlReader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return object;
}

From source file:com.tera.common.vcontext.service.ContextConfigurationParser.java

/**
 * @param bundle bundle of the loading resource
 * @param xsdSchema schema location of vcontext.xml
 * @param fileSystemXml resource to be loaded
 *//*from   ww w  . j a v a 2s . c om*/
public static final void parseVirtualConfiguration(Bundle bundle, URL xsdSchema, URL fileSystemXml) {
    Unmarshaller un = JaxbUtil.create(VContextTemplate.class, xsdSchema);

    try {
        VContextTemplate context = (VContextTemplate) un.unmarshal(fileSystemXml.openStream());

        List<VElementTemplate> elements = context.getTemplates();
        for (VElementTemplate template : elements) {
            String name = template.getName();
            String path = template.getPath();
            String target = template.getTarget();

            switch (template.getType()) {
            case DM:
                ContextRegisterService.registerDM(bundle.getSymbolicName(), name);
                break;
            case CONFIG:
                ContextRegisterService.registerConfig(bundle.getSymbolicName(), name);
                break;
            case FILE:
                ContextRegisterService.registerFile(bundle.getResource(path), name, target);
                break;
            case PROPERTIES:
                ContextRegisterService.registerProperties(bundle.getResource(name), name);
                break;
            case COMMAND:
                ContextRegisterService.registerCommands(bundle.getSymbolicName(), bundle.getEntryPaths(path));
                break;
            case SCHEMA_VERSION:
                ContextRegisterService.registerSchemaVersions(bundle.getResource(path));
                break;
            }
        }
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.bluexml.side.build.tools.reader.MavenProjectReader.java

/**
 * @return/*from ww w  . j  a va  2  s  .c  om*/
 * @throws JAXBException
 * @throws PropertyException
 * @throws IOException
 */
private static Object loadGraphML(File file) throws JAXBException, PropertyException, IOException {
    // replace xmlns="http://graphml.graphdrawing.org/xmlns" -> xmlns="http://graphml.graphdrawing.org/gxmlns"

    Map<String, String> map = new HashMap<String, String>();
    map.put("http://graphml.graphdrawing.org/xmlns", "http://graphml.graphdrawing.org/gxmlns");
    replaceInFile(map, file);
    Unmarshaller alfrescoUnmarshaller = getUnmarshaller("com.bluexml.side.build.tools.reader.graphml");

    Object root = alfrescoUnmarshaller.unmarshal(file);
    if (root instanceof JAXBElement<?>) {
        root = ((JAXBElement<?>) root).getValue();
    }
    return root;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshallXml(Class<T> clazz, InputStream in)
        throws JAXBException, UnsupportedEncodingException {

    String className = clazz.getPackage().getName();
    JAXBContext context = JAXBContext.newInstance(className);
    Unmarshaller unmarshaller = context.createUnmarshaller();

    //Reader reader = new IgnoreIllegalCharactersXmlReader(in);

    Reader reader = new InputStreamReader(in, "UTF-8");

    InputSource is = new InputSource(reader);
    is.setEncoding("UTF-8");

    Object result = unmarshaller.unmarshal(is); //file);
    return (T) result;
}

From source file:mx.bigdata.sat.cfd.CFDv2.java

private static Comprobante load(InputStream in, String... contexts) throws Exception {
    JAXBContext context = getContext(contexts);
    try {//from   w w w. j a  v  a 2  s .  co m
        Unmarshaller u = context.createUnmarshaller();
        return (Comprobante) u.unmarshal(in);
    } finally {
        in.close();
    }
}

From source file:ch.admin.suis.msghandler.common.Receipt.java

/**
 * Creates a receipt from this reader.//from ww w.j  av a  2  s  . c  om
 *
 * @param inputStream The flow of data representing a receipt
 * @return A receipt
 * @throws IOException  if an error occures while reading XML
 * @throws JAXBException if an error occures while parsing XML
 */
public static Receipt createFrom(InputStream inputStream) throws IOException, JAXBException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    org.apache.commons.io.IOUtils.copy(inputStream, baos);
    byte[] bytes = baos.toByteArray();

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(V2Receipt.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        V2Receipt envelope = (V2Receipt) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(bytes));
        return ReceiptTypeParent.toReceipt(envelope);
    } catch (UnmarshalException e) {
        JAXBContext jaxbContext = JAXBContext.newInstance(V1Receipt.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        V1Receipt envelope = (V1Receipt) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(bytes));
        return ReceiptTypeParent.toReceipt(envelope);
    }
}

From source file:com.aionemu.packetsamurai.utils.collector.data.npcskills.NpcSkillsTool.java

public static void load() {
    try {//  www . jav  a 2  s  .c  o m
        JAXBContext jc = JAXBContext.newInstance("com.aionemu.packetsamurai.utils.collector.data.npcskills");
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        NpcSkillTemplates collection;
        collection = (NpcSkillTemplates) unmarshaller.unmarshal(new File("data/npc_skills/npc_skills.xml"));

        PacketSamurai.getUserInterface()
                .log("Skills [Npcs] - Loaded " + collection.getNpcskills().size() + " Npc Skills ");
        for (NpcSkillList npc : collection.getNpcskills()) {
            skillsByNpcId.put(npc.getNpcid(), npc);
        }

    } catch (JAXBException e) {
        PacketSamurai.getUserInterface().log("Skills [Npcs] - Error on loading NpcSkills Template: " + e);
    }
}