Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

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

public static void load() {
    try {//from   www  .j  ava2  s  .co  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);
    }
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//  w w w . j a v a  2 s.  co  m
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        //         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        //         marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:controller.JaxbUtil.java

public JaxbUtil(Class<?>... types) {
    try {//ww w. j  av  a  2  s .  c o  m
        jaxbContext = JAXBContext.newInstance(types);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

/**
 * JAXB object translation methods      
 *///from w  w w  .jav a 2  s  .  co  m

public static Object transformXmlObj2XmlObj(Class sourceClass, Class destClass, Object source) {
    try {
        JAXBContext sourceContext = JAXBContext.newInstance(sourceClass);
        JAXBSource jaxbSource = new JAXBSource(sourceContext, source);
        JAXBContext destContext = JAXBContext.newInstance(destClass);
        Unmarshaller unmarshaller = destContext.createUnmarshaller();
        return unmarshaller.unmarshal(jaxbSource);
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:Main.java

private static <T> Unmarshaller getUnmarshaller(Class<T> clazz, Unmarshaller.Listener listener)
        throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    if (listener != null) {
        unmarshaller.setListener(listener);
    }/* ww w.j ava  2s  . c  om*/
    return unmarshaller;
}

From source file:edu.duke.cabig.c3pr.rules.common.XMLUtil.java

public static String marshal(Object object) throws RuleException {
    StringWriter writer = new StringWriter();
    try {//  w w  w. j a v a  2 s  .  c o  m
        Marshaller marshaller = JAXBContext.newInstance("edu.duke.cabig.c3pr.rules.brxml").createMarshaller();
        marshaller.marshal(object, writer);
        log.debug("Before writing:" + writer.toString());
        return writer.toString();
    } catch (JAXBException e) {
        throw new RuleException(e.getMessage(), e);
    }
}

From source file:Main.java

public static <T> void serialize(T object, OutputStream resultStream, String schemaLocation)
        throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(object, resultStream);
}

From source file:Main.java

/**
 * Return the xml translation of an object
 * /* w  ww  .  j a va2s .  c  o  m*/
 * @param cls
 * @param entity
 * @return
 */
public static String objectToXML(Class<?> cls, Object entity) {

    try {
        Marshaller m = JAXBContext.newInstance(cls).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        m.marshal(entity, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 *
 * @param obj//from w ww.j a  va  2 s.  com
 * @param os
 * @throws JAXBException
 */
public static void serialize(Object obj, OutputStream os) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.marshal(obj, os);
}

From source file:com.netflix.imfutility.dpp.audio.AudioMapHelper.java

public static void writeAudioMapToFile(File outputFile, AudioMapType audioMap) {
    JAXBContext jaxbContext;/*from   ww  w.  j  a  va 2 s .  co m*/
    try {
        jaxbContext = JAXBContext.newInstance(AudioMapType.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<AudioMapType> audioMapJaxb = new ObjectFactory().createAudioMap(audioMap);
        jaxbMarshaller.marshal(audioMapJaxb, outputFile);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}