当前位置: 首页>>代码示例>>Java>>正文


Java JAXBContext.createMarshaller方法代码示例

本文整理汇总了Java中javax.xml.bind.JAXBContext.createMarshaller方法的典型用法代码示例。如果您正苦于以下问题:Java JAXBContext.createMarshaller方法的具体用法?Java JAXBContext.createMarshaller怎么用?Java JAXBContext.createMarshaller使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.xml.bind.JAXBContext的用法示例。


在下文中一共展示了JAXBContext.createMarshaller方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: writeTo

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders,
		OutputStream entityStream) throws IOException, WebApplicationException {
	try {
		JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
		Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
		jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
		for (Annotation annotation : annotations) {
			if (annotation instanceof XsiSchemaLocation) {
				XsiSchemaLocation schemaAnnotation = (XsiSchemaLocation) annotation;
				String schemaLocation = schemaAnnotation.schemaLocation();
				jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
			}
		}
		XmlType xmlTypeAnnotation = object.getClass().getAnnotation(XmlType.class);
		QName qname = new QName("", xmlTypeAnnotation.name());
		StringWriter stringWriter = new StringWriter();
		JAXBElement<Object> jaxbElement = new JAXBElement<Object>(qname, (Class<Object>) object.getClass(), null, object);
		jaxbMarshaller.marshal(jaxbElement, stringWriter);
		entityStream.write(stringWriter.toString().getBytes());
	} catch (Exception e) {
		throw new WebApplicationException(e);
	}
}
 
开发者ID:SoapboxRaceWorld,项目名称:soapbox-race-core,代码行数:27,代码来源:MarshallerInterceptor.java

示例2: convertToXML

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
/**
 * Object to XML
 * @param object object
 * @return xml
 */
public static String convertToXML(Object object){
	try {
		Map<Class<?>, Marshaller> mMap = mMapLocal.get();
		if(!mMap.containsKey(object.getClass())){
			JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			//设置CDATA输出字符
			marshaller.setProperty(CharacterEscapeHandler.class.getName(), new CharacterEscapeHandler() {
				public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
					writer.write(ac, i, j);
				}
			});
			mMap.put(object.getClass(), marshaller);
		}
		StringWriter stringWriter = new StringWriter();
		mMap.get(object.getClass()).marshal(object,stringWriter);
		return stringWriter.getBuffer().toString();
	} catch (JAXBException e) {
		e.printStackTrace();
	}
	return null;
}
 
开发者ID:luotuo,项目名称:springboot-security-wechat,代码行数:29,代码来源:XMLConverUtil.java

示例3: marshallToString

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
private static String marshallToString(Object object,
        boolean includeXMLHeader) throws MarshalException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
            100);
    try {
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, !includeXMLHeader);
        marshaller.marshal(object, byteArrayOutputStream);
        return new String(byteArrayOutputStream.toByteArray());
    } catch (Throwable e) {
        throw new MarshalException("Unable to marshal object to stream", e);
    } finally {
        IOUtils.closeQuietly(byteArrayOutputStream);
    }
}
 
开发者ID:webinerds,项目名称:s3-proxy-chunk-upload,代码行数:17,代码来源:JAXBUtils.java

示例4: createMarshaller

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
/**
 * 创建Marshaller并设定encoding(可为null).
 * 线程不安全,需要每次创建或pooling。
 */
public static Marshaller createMarshaller(Class clazz, String encoding) {
	try {
		JAXBContext jaxbContext = getJaxbContext(clazz);

		Marshaller marshaller = jaxbContext.createMarshaller();

		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

		if (StringUtils.isNotBlank(encoding)) {
			marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
		}

		return marshaller;
	} catch (JAXBException e) {
		throw Exceptions.unchecked(e);
	}
}
 
开发者ID:funtl,项目名称:framework,代码行数:22,代码来源:JaxbMapper.java

示例5: saveExamToFile

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
public static void saveExamToFile(File file,Exam exam){
	try {
		//prepare the marshaller
		JAXBContext context = JAXBContext.newInstance(Exam.class);
		Marshaller m = context.createMarshaller();
		//Save the exam to a file and print it pretty on the console
		m.marshal(exam,file);
		//prettify here to save space in the file
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		System.out.println(exam);
		m.marshal(exam,System.out);
		MainApp.currentFilePath = file.getAbsolutePath();

	}catch (Exception e){
		e.printStackTrace();
	}
}
 
开发者ID:eacp,项目名称:Luna-Exam-Builder,代码行数:18,代码来源:Data.java

示例6: log

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
private void log(Collection<IPersistent> objectsToLog, Collection<String> serializedObjects)
throws ApplicationExceptions, FrameworkException {
    // NOTE: This is for demo purposes only.
    // It serializes the domain objects to XML and adds the resulting String to the output collection
    // A production-level implementation may probably add the resulting String to the database
    // In which case, do rememeber to invoke the flush() method on the UOW!
    if (objectsToLog != null) {
        try {
            for (IPersistent obj : objectsToLog) {
                StringWriter writer = new StringWriter();
                JAXBContext jc = JAXBContext.newInstance(new Class[] {obj.getClass()});
                Marshaller marshaller = jc.createMarshaller();
                marshaller.marshal(obj, writer);
                serializedObjects.add(writer.toString());
                writer.close();
            }
        } catch (Exception e) {
            throw ExceptionHelper.throwAFR(e);
        }
    }
}
 
开发者ID:jaffa-projects,项目名称:jaffa-framework,代码行数:22,代码来源:DemoLogger.java

示例7: eventToXML

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
private String eventToXML(Event event) throws JAXBException {
	JAXBEvent jaxbEvent = new JAXBEvent(event);
	StringWriter xmlString = new StringWriter();
	JAXBContext context = JAXBContext.newInstance(JAXBEvent.class);
	Marshaller marshallerObj = context.createMarshaller();
	marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
	marshallerObj.marshal(jaxbEvent, xmlString);
	return xmlString.toString();
}
 
开发者ID:edgexfoundry,项目名称:export-distro,代码行数:10,代码来源:XMLFormatTransformer.java

示例8: saveToXml

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
public void saveToXml(File file) throws JAXBException
{
    JAXBContext context;
    context = JAXBContext.newInstance(this.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    marshaller.marshal(this, file);

}
 
开发者ID:Matthieu42,项目名称:Steam-trader-tools,代码行数:11,代码来源:UserAppList.java

示例9: createMarshaller

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
/**
 * Creates a new {@link javax.xml.bind.Marshaller} that handles the supplied class.
 */
public Marshaller createMarshaller(Class<?> clazz) throws JAXBException {
  JAXBContext ctx = getContext(clazz);
  Marshaller marshaller = ctx.createMarshaller();
  setMarshallerProperties(marshaller);
  return marshaller;
}
 
开发者ID:wenwu315,项目名称:XXXX,代码行数:10,代码来源:JAXBContextFactory.java

示例10: process

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
@Override
	public void process(ProcessingContext<Corpus> ctx, Corpus corpus) throws ModuleException {
		Logger logger = getLogger(ctx);

		try {		
//			logger.info("creating the External module object ");
			TEESClassifyExternal teesClassifyExt = new TEESClassifyExternal(ctx);

//			logger.info("producing a interaction xml ");
			JAXBContext jaxbContext = JAXBContext.newInstance(CorpusTEES.class);
			Marshaller jaxbm = jaxbContext.createMarshaller();
			jaxbm.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			jaxbm.marshal(this.prepareTEESCorpora(ctx, corpus), teesClassifyExt.getInput());

			logger.info("Predicting with TEES");
			callExternal(ctx, "run-tees-predict", teesClassifyExt, INTERNAL_ENCODING, "tees-classify.py");

			logger.info("Reading TEES output");
			Unmarshaller jaxbu = jaxbContext.createUnmarshaller();
			CorpusTEES corpusTEES = (CorpusTEES) jaxbu.unmarshal(teesClassifyExt.getPredictionFile());

//			logger.info("adding detected relations to Corpus ");
			setRelations2CorpusAlvis(corpusTEES);
			
			logger.info("number of documents : " + corpusTEES.getDocument().size());
		}
		catch (JAXBException|IOException e) {
			rethrow(e);
		}
	}
 
开发者ID:Bibliome,项目名称:alvisnlp,代码行数:31,代码来源:TEESClassify.java

示例11: toXml

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
private static String toXml(Statistics stats) {
	try {
		JAXBContext context = JAXBContext.newInstance(StatisticsEntity.class);
		Marshaller marshaller = context.createMarshaller();
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		StringWriter writer = new StringWriter();
		marshaller.marshal(StatisticsEntity.from(stats), writer);
		return writer.toString();
	} catch (JAXBException ex) {
		// don't do this in real live
		return ex.toString();
	}
}
 
开发者ID:CodeFX-org,项目名称:demo-java-9-migration,代码行数:14,代码来源:MonitorServer.java

示例12: marshallClassCastExceptionTest

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
@Test
public void marshallClassCastExceptionTest() throws Exception {
    JAXBContext jaxbContext;
    Marshaller marshaller;
    URLClassLoader jaxbContextClassLoader;
    // Generate java classes by xjc
    runXjc(XSD_FILENAME);
    // Compile xjc generated java files
    compileXjcGeneratedClasses();

    // Create JAXB context based on xjc generated package.
    // Need to create URL class loader ot make compiled classes discoverable
    // by JAXB context
    jaxbContextClassLoader = URLClassLoader.newInstance(new URL[] {testWorkDirUrl});
    jaxbContext = JAXBContext.newInstance( TEST_PACKAGE, jaxbContextClassLoader);

    // Create instance of Xjc generated data type.
    // Java classes were compiled during the test execution hence reflection
    // is needed here
    Class classLongListClass = jaxbContextClassLoader.loadClass(TEST_CLASS);
    Object objectLongListClass = classLongListClass.newInstance();
    // Get 'getIn' method object
    Method getInMethod = classLongListClass.getMethod( GET_LIST_METHOD, (Class [])null );
    // Invoke 'getIn' method
    List<Long> inList = (List<Long>)getInMethod.invoke(objectLongListClass);
    // Add values into the jaxb object list
    inList.add(Long.valueOf(0));
    inList.add(Long.valueOf(43));
    inList.add(Long.valueOf(1000000123));

    // Marshall constructed complex type variable to standard output.
    // In case of failure the ClassCastException will be thrown
    marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(objectLongListClass, System.out);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:37,代码来源:JaxbMarshallTest.java

示例13: serializeAndDeserializeXML

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
@Test
public void serializeAndDeserializeXML() throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(ProblemResponse.class);
    Marshaller marshaller = jc.createMarshaller();
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    marshaller.marshal(problemResponse, output);
    ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());

    ProblemResponse unmarshal = (ProblemResponse) unmarshaller.unmarshal(input);
    assertThat(unmarshal, is(equalTo(problemResponse)));
}
 
开发者ID:code-obos,项目名称:servicebuilder,代码行数:14,代码来源:ProblemResponseParserTest.java

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

	FileOutputStream fos = new FileOutputStream("target/simpleReport.pdf");
	service.generateSimpleReport(writer.toString(), fos);
}
 
开发者ID:esig,项目名称:dss-demonstrations,代码行数:16,代码来源:FOPServiceTest.java

示例15: serializeIt

import javax.xml.bind.JAXBContext; //导入方法依赖的package包/类
private String serializeIt(ServicePaths servicePaths, Boolean format) throws Exception {
    JAXBContext context = JAXBContext.newInstance(ServicePaths.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);

    StringWriter result = new StringWriter();
    marshaller.marshal(servicePaths, result);

    return result.toString();
}
 
开发者ID:Comcast,项目名称:redirector,代码行数:11,代码来源:ServicePathsTest.java


注:本文中的javax.xml.bind.JAXBContext.createMarshaller方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。