org.springframework.oxm.xstream.XStreamMarshaller Java Examples

The following examples show how to use org.springframework.oxm.xstream.XStreamMarshaller. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XmlFileItemWriterDemo.java    From SpringAll with MIT License 6 votes vote down vote up
private StaxEventItemWriter<TestData> xmlFileItemWriter() throws IOException {
    StaxEventItemWriter<TestData> writer = new StaxEventItemWriter<>();

    // 通过XStreamMarshaller将TestData转换为xml
    XStreamMarshaller marshaller = new XStreamMarshaller();

    Map<String,Class<TestData>> map = new HashMap<>(1);
    map.put("test", TestData.class);

    marshaller.setAliases(map); // 设置xml标签

    writer.setRootTagName("tests"); // 设置根标签
    writer.setMarshaller(marshaller);

    FileSystemResource file = new FileSystemResource("/Users/mrbird/Desktop/file.xml");
    Path path = Paths.get(file.getPath());
    if (!Files.exists(path)) {
        Files.createFile(path);
    }

    writer.setResource(file); // 设置目标文件路径
    return writer;
}
 
Example #2
Source File: ItemWriterConfigure.java    From SpringAll with MIT License 6 votes vote down vote up
@Bean
public StaxEventItemWriter<TestData> xmlFileItemWriter() throws Exception {
    StaxEventItemWriter<TestData> writer = new StaxEventItemWriter<>();

    // 通过XStreamMarshaller将TestData转换为xml
    XStreamMarshaller marshaller = new XStreamMarshaller();

    Map<String, Class<TestData>> map = new HashMap<>(1);
    map.put("test", TestData.class);

    marshaller.setAliases(map); // 设置xml标签

    writer.setRootTagName("tests"); // 设置根标签
    writer.setMarshaller(marshaller);

    FileSystemResource file = new FileSystemResource("/Users/mrbird/Desktop/file.xml");
    Path path = Paths.get(file.getPath());
    if (!Files.exists(path)) {
        Files.createFile(path);
    }

    writer.setResource(file); // 设置目标文件路径
    return writer;
}
 
Example #3
Source File: UnmarshallXmlTest.java    From cia with Apache License 2.0 6 votes vote down vote up
/**
 * Unmarshall test.
 *
 * @throws Exception
 *             the exception
 */
@Test
public void unmarshallTest() throws Exception {
	final String content = "abcdefg";
	final String xmlContent = "<string>" + content + "</string>";
	File temp;
	temp = File.createTempFile("UnmarshallXmlTestTempFile", ".xml");

	final OutputStreamWriter bw = new OutputStreamWriter(new FileOutputStream(temp), StandardCharsets.UTF_8);
	bw.write(xmlContent);
	bw.close();

	final XStream xstream = new XStream();
	xstream.alias("TestXml", TestXml.class);
	assertEquals(content, unmarshallXml(new XStreamMarshaller(), temp.getAbsolutePath()));
}
 
Example #4
Source File: XmlFileItemReaderDemo.java    From SpringAll with MIT License 5 votes vote down vote up
private ItemReader<TestData> xmlFileItemReader() {
    StaxEventItemReader<TestData> reader = new StaxEventItemReader<>();
    reader.setResource(new ClassPathResource("file.xml")); // 设置xml文件源
    reader.setFragmentRootElementName("test"); // 指定xml文件的根标签
    // 将xml数据转换为TestData对象
    XStreamMarshaller marshaller = new XStreamMarshaller();
    // 指定需要转换的目标数据类型
    Map<String, Class<TestData>> map = new HashMap<>(1);
    map.put("test", TestData.class);
    marshaller.setAliases(map);

    reader.setUnmarshaller(marshaller);
    return reader;
}
 
Example #5
Source File: MvcConfig.java    From tutorials with MIT License 5 votes vote down vote up
private HttpMessageConverter<Object> createXmlHttpMessageConverter() {
    final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter();

    final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller();
    xmlConverter.setMarshaller(xstreamMarshaller);
    xmlConverter.setUnmarshaller(xstreamMarshaller);

    return xmlConverter;
}