當前位置: 首頁>>代碼示例>>Java>>正文


Java JAXBContext類代碼示例

本文整理匯總了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;
}
 
開發者ID:phweda,項目名稱:MFM,代碼行數:23,代碼來源:MAMEtoJTree.java

示例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");
	}

}
 
開發者ID:theits,項目名稱:CharmMylynConnector,代碼行數:17,代碼來源:CharmAttachmentPostWriter.java

示例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);
  }
}
 
開發者ID:gurkenlabs,項目名稱:litiengine,代碼行數:21,代碼來源:GameFile.java

示例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());
}
 
開發者ID:PacktPublishing,項目名稱:Java-9-Cookbook,代碼行數:19,代碼來源:HelloWorldXml.java

示例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);
}
 
開發者ID:hashsdn,項目名稱:hashsdn-controller,代碼行數:18,代碼來源:FeatureConfigSnapshotHolder.java

示例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;

}
 
開發者ID:EnFlexIT,項目名稱:AgentWorkbench,代碼行數:26,代碼來源:ProjectExportController.java

示例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");
	}

}
 
開發者ID:theits,項目名稱:CharmMylynConnector,代碼行數:17,代碼來源:CharmTaskMessageBodyWriter.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:JAXBContextFactory.java

示例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;
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:25,代碼來源:JaxbParser.java

示例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;
}
 
開發者ID:Yarichi,項目名稱:Proyecto-DASI,代碼行數:22,代碼來源:SchemaHelper.java

示例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);
}
 
開發者ID:esig,項目名稱:dss-demonstrations,代碼行數:17,代碼來源:XSLTServiceTest.java

示例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);
		}
	}
}
 
開發者ID:camy2408,項目名稱:automatic-mower,代碼行數:20,代碼來源:MowerStatusChangedXMLReport.java

示例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);
	}

}
 
開發者ID:jesuino,項目名稱:kie-ml,代碼行數:17,代碼來源:KieMLContainerImpl.java

示例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");
	}

}
 
開發者ID:theits,項目名稱:CharmMylynConnector,代碼行數:18,代碼來源:CharmTaskMessageBodyReader.java

示例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);
    
}
 
開發者ID:nmajorov,項目名稱:keycloak_training,代碼行數:17,代碼來源:BindTest.java


注:本文中的javax.xml.bind.JAXBContext類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。