本文整理汇总了Java中javax.xml.bind.JAXBContext类的典型用法代码示例。如果您正苦于以下问题:Java JAXBContext类的具体用法?Java JAXBContext怎么用?Java JAXBContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JAXBContext类属于javax.xml.bind包,在下文中一共展示了JAXBContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: JAXB
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
/**
* This is simply to provide MAME input to test this class
*/
private static Mame JAXB() throws JAXBException, MAMEexe.MAME_Exception {
Mame mame = null;
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Phweda.MFM.mame.Mame.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
// hard code path to mame exe
MAMEexe.setBaseArgs("E:\\Test\\177\\mame64.exe");
ArrayList<String> args = new ArrayList<String>(Arrays.asList("-listxml", "*"));
Process process = MAMEexe.run(args);
InputStream inputStream = process.getInputStream();
mame = (Mame) jaxbUnmarshaller.unmarshal(inputStream);
System.out.println("Machines" + mame.getMachine().size());
} catch (JAXBException | MAMEexe.MAME_Exception e) {
e.printStackTrace();
throw e;
}
return mame;
}
示例2: writeTo
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
@Override
public void writeTo(CharmAttachmentPost attachment, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext context = JAXBContext.newInstance(CharmAttachmentPost.class);
Marshaller taskMarshaller = context.createMarshaller();
taskMarshaller.marshal(attachment, entityStream);
} catch (JAXBException e) {
throw new ProcessingException("Error serializing Attachment to output stream");
}
}
示例3: getGameFileFromFile
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
private static GameFile getGameFileFromFile(String file) throws JAXBException, IOException {
final JAXBContext jaxbContext = JAXBContext.newInstance(GameFile.class);
final Unmarshaller um = jaxbContext.createUnmarshaller();
try (InputStream inputStream = FileUtilities.getGameResource(file)) {
// try to get compressed game file
final GZIPInputStream zipStream = new GZIPInputStream(inputStream);
return (GameFile) um.unmarshal(zipStream);
} catch (final ZipException e) {
// if it fails to load the compressed file, get it from plain XML
InputStream stream = null;
stream = FileUtilities.getGameResource(file);
if (stream == null) {
return null;
}
return (GameFile) um.unmarshal(stream);
}
}
示例4: main
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
public static void main(String[] args) throws JAXBException{
//create instance of JAXBContext with the class we want to serialize into XML
JAXBContext jaxb = JAXBContext.newInstance(Messages.class);
//create a marshaller which will do the task of generating xml
Marshaller marshaller = jaxb.createMarshaller();
//setting the property of marshaller to not add the <? xml> tag
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
StringWriter writer = new StringWriter();
//serialze the Messages instance and send the string to the writer
marshaller.marshal(new Messages(), writer);
//get the XML from the writer
System.out.println(writer.toString());
}
示例5: FeatureConfigSnapshotHolder
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
public FeatureConfigSnapshotHolder(final ConfigFileInfo fileInfo,
final Feature feature) throws JAXBException, XMLStreamException {
Preconditions.checkNotNull(fileInfo);
Preconditions.checkNotNull(fileInfo.getFinalname());
Preconditions.checkNotNull(feature);
this.fileInfo = fileInfo;
this.featureChain.add(feature);
// TODO extract utility method for umarshalling config snapshots
JAXBContext jaxbContext = JAXBContext.newInstance(ConfigSnapshot.class);
Unmarshaller um = jaxbContext.createUnmarshaller();
XMLInputFactory xif = XMLInputFactory.newFactory();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File(fileInfo.getFinalname())));
unmarshalled = (ConfigSnapshot) um.unmarshal(xsr);
}
示例6: loadSimSetup
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
/**
* Loads the simulation setup with the specified name
* @param setupName The setup to be loaded
* @return The setup
* @throws JAXBException Parsing the setup file failed
* @throws IOException Reading the setup file failed
*/
private SimulationSetup loadSimSetup(String setupName) throws JAXBException, IOException {
// --- Determine the setup file path ----------
String setupFileName = this.project.getSimulationSetups().get(setupName);
String setupFileFullPath = this.project.getSubFolder4Setups(true) + File.separator + setupFileName;
File setupFile = new File(setupFileFullPath);
// --- Load the setup -------------
JAXBContext pc;
SimulationSetup simSetup = null;
pc = JAXBContext.newInstance(this.project.getSimulationSetups().getCurrSimSetup().getClass());
Unmarshaller um = pc.createUnmarshaller();
FileReader fr = new FileReader(setupFile);
simSetup = (SimulationSetup) um.unmarshal(fr);
fr.close();
return simSetup;
}
示例7: writeTo
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
@Override
public void writeTo(CharmTask charmTask, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext context = JAXBContext.newInstance(CharmTask.class);
Marshaller taskMarshaller = context.createMarshaller();
taskMarshaller.marshal(charmTask, entityStream);
} catch (JAXBException e) {
throw new ProcessingException("Error serializing Charm Task to output stream");
}
}
示例8: createContext
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
/**
* The JAXB API will invoke this method via reflection
*/
public static JAXBContext createContext( String contextPath,
ClassLoader classLoader, Map properties ) throws JAXBException {
List<Class> classes = new ArrayList<Class>();
StringTokenizer tokens = new StringTokenizer(contextPath,":");
// each package should be pointing to a JAXB RI generated
// content interface package.
//
// translate them into a list of private ObjectFactories.
try {
while(tokens.hasMoreTokens()) {
String pkg = tokens.nextToken();
classes.add(classLoader.loadClass(pkg+IMPL_DOT_OBJECT_FACTORY));
}
} catch (ClassNotFoundException e) {
throw new JAXBException(e);
}
// delegate to the JAXB provider in the system
return JAXBContext.newInstance(classes.toArray(new Class[classes.size()]),properties);
}
示例9: toXML
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
/**
* 转为xml串
*
* @param obj
* @return
*/
public String toXML(Object obj) {
String result = null;
try {
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉报文头
OutputStream os = new ByteArrayOutputStream();
XMLSerializer serializer = getXMLSerializer(os);
m.marshal(obj, serializer.asContentHandler());
result = os.toString();
} catch (Exception e) {
e.printStackTrace();
}
logger.info("response text:" + result);
return result;
}
示例10: getJAXBContext
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
/** Serialise the object to an XML string
* @param obj the object to be serialised
* @param objclass the class of the object to be serialised
* @return an XML string representing the object, or null if the object couldn't be serialised
* @throws JAXBException
*/
static private JAXBContext getJAXBContext(Class<?> objclass) throws JAXBException
{
JAXBContext jaxbContext;
if (jaxbContentCache.containsKey(objclass.getName()))
{
jaxbContext = jaxbContentCache.get(objclass.getName());
}
else
{
jaxbContext = JAXBContext.newInstance(objclass);
jaxbContentCache.put(objclass.getName(), jaxbContext);
}
return jaxbContext;
}
示例11: generateSimpleReport
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
@Test
public void generateSimpleReport() throws Exception {
JAXBContext context = JAXBContext.newInstance(SimpleReport.class.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
Marshaller marshaller = context.createMarshaller();
SimpleReport simpleReport = (SimpleReport) unmarshaller.unmarshal(new File("src/test/resources/simpleReport.xml"));
assertNotNull(simpleReport);
StringWriter writer = new StringWriter();
marshaller.marshal(simpleReport, writer);
String htmlSimpleReport = service.generateSimpleReport(writer.toString());
assertTrue(Utils.isStringNotEmpty(htmlSimpleReport));
logger.debug("Simple report html : " + htmlSimpleReport);
}
示例12: sendAlert
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
@Override
public void sendAlert(Observable observable) {
if (observable instanceof AbstarctMower) {
try {
AbstarctMower mower = (AbstarctMower) observable;
SettingInterface settingLoader = SettingLoaderFactory.getSettingLoader(DEFAULT);
StringBuilder br = new StringBuilder(settingLoader.getSerFolder());
br.append(BACKSLASH).append(settingLoader.getReportingFolder()).append(BACKSLASH)
.append(mower.getIdentifier()).append(new Date().getTime()).append(XML_EXTENTION);
File file = new File(br.toString());
JAXBContext jaxbContext = JAXBContext.newInstance(AbstarctMower.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(mower, file);
} catch (Exception e) {
throw new MowerException(e);
}
}
}
示例13: loadModels
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
private void loadModels() {
ClassLoader cl = kieContainer.getClassLoader();
InputStream modelsIS = cl.getResourceAsStream(MODEL_DESCRIPTOR_PATH);
if (modelsIS == null) {
throw new IllegalArgumentException("Not able to read descriptor" + MODEL_DESCRIPTOR_PATH);
}
try {
JAXBContext context = JAXBContext.newInstance(Model.class, ModelList.class, ModelParam.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
models = (ModelList) unmarshaller.unmarshal(modelsIS);
models.getModels().stream().filter(m -> m.getModelLabelsPath() != null).forEach(m -> m.setLabels(loadLabelsForModel(m)));
} catch (JAXBException e) {
throw new IllegalArgumentException("Not able to unmarshall descriptor" + MODEL_DESCRIPTOR_PATH, e);
}
}
示例14: readFrom
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
@Override
public CharmTask readFrom(Class<CharmTask> type, Type genericType, Annotation[] annotations, MediaType mediaType,
MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
throws IOException, WebApplicationException {
try {
JAXBContext context = JAXBContext.newInstance(CharmTask.class);
Unmarshaller taskUnmarshaller = context.createUnmarshaller();
return (CharmTask) taskUnmarshaller.unmarshal(entityStream);
} catch (JAXBException e) {
throw new ProcessingException("Error deserializing Task");
}
}
示例15: testMarschallingMessage
import javax.xml.bind.JAXBContext; //导入依赖的package包/类
@Test
public void testMarschallingMessage() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Message.class);
// Create the Marshaller Object using the JaxB Context
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT,false);
Message message = new Message();
message.setMessage("success");
// Marshal the employee object to JSON and print the output to console
marshaller.marshal(message, System.out);
}