StaxItemReader 读取和打印值 [英] StaxItemReader to read and print the values

查看:57
本文介绍了StaxItemReader 读取和打印值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习春季批次.我想使用 StaxEventItemReader 来读取 xml 文件所以我只是尝试在 java 透视图中的独立 java 文件中使用它和所有必要的 spring jar.

我想知道如何确保它是否读取了值以及读取了哪些值.简而言之,我想在 console 中打印读取值.我怎样才能在独立的java文件中做到这一点?

代码如下:

 main(){StaxEventItemReader<学生>xmlFileReader = new StaxEventItemReader();xmlFileReader.setResource(new ClassPathResource("/Student.xml"));xmlFileReader.setFragmentRootElementName("标记");Jaxb2Marshaller medicareMarshaller = new Jaxb2Marshaller();medicareMarshaller.setClassesToBeBound(Student.class);xmlFileReader.setUnmarshaller(medicareMarshaller);System.out.println(xmlFileReader.?);}

请帮助我了解如何打印读取值.如果我的内容不清楚,我深表歉意.提前致谢.

解决方案

试试下面我改编的代码.通常,如果您想测试 SpringBatch 组件而不将它们实例化为 Spring-Beans(-> 通过直接调用 der Constructor),您应该在调用所有 set-Methods 之后调用afterPropertiesSet()"方法.

接下来,根据您要直接测试的读写器,您必须调用 reader.open(new ExecutionContext()).

之后,您可以调用 read() 方法,该方法应该会逐项返回.

main(){StaxEventItemReader<学生>xmlFileReader = new StaxEventItemReader();xmlFileReader.setResource(new ClassPathResource("/Student.xml"));xmlFileReader.setFragmentRootElementName("标记");Jaxb2Marshaller medicareMarshaller = new Jaxb2Marshaller();medicareMarshaller.setClassesToBeBound(Student.class);xmlFileReader.setUnmarshaller(medicareMarshaller);xmlFileReader.afterPropertiesSet();//在 StaxEventItemReader 的情况下不是真正必要的xmlFileReader.open(new ExecutionContext());//做了一些初始化,所以你需要调用它学生学生 = null;而(学生 = xmlFileReader.read() != null){System.out.println(学生...);}xmlFileReader.close();

I am learning spring batch now. I wanted to use StaxEventItemReaderto read xml file So I just tried using it in standalone java file in java perspective with all necessary spring jars.

I want to know how can I ensure whether it has read the values and what values it has read . In short I want to print the read values in console . How can I do it in standalone java file?

Code as follows:

    main(){
    StaxEventItemReader<Student> xmlFileReader = new StaxEventItemReader<Student>();
    xmlFileReader.setResource(new ClassPathResource("/Student.xml"));
    xmlFileReader.setFragmentRootElementName("Marks");

    Jaxb2Marshaller medicareMarshaller = new Jaxb2Marshaller();
    medicareMarshaller.setClassesToBeBound(Student.class);
    xmlFileReader.setUnmarshaller(medicareMarshaller);
    System.out.println(xmlFileReader. ?);
}

Please help me in knowing how to print the read values. I apologise if my content is not clear. Thanks in Advance.

解决方案

Try my adapted code below. Generally, if you want to test SpringBatch components without having them instantiated as Spring-Beans (-> by calling der Constructor directly), you should call the "afterPropertiesSet()" method, after you have called all your set-Methods.

Next, depending on the reader/writers you want to test directly, you have to call reader.open(new ExecutionContext()).

After that, you can call the read()-method, which should then return item after item.

main(){
StaxEventItemReader<Student> xmlFileReader = new StaxEventItemReader<Student>();
xmlFileReader.setResource(new ClassPathResource("/Student.xml"));
xmlFileReader.setFragmentRootElementName("Marks");

Jaxb2Marshaller medicareMarshaller = new Jaxb2Marshaller();
medicareMarshaller.setClassesToBeBound(Student.class);
xmlFileReader.setUnmarshaller(medicareMarshaller);

xmlFileReader.afterPropertiesSet(); // in the case of StaxEventItemReader not really necessary

xmlFileReader.open(new ExecutionContext()); // does some initialisation, so you need to call it

Studen student = null;
while(student = xmlFileReader.read() != null) {
    System.out.println(student...);   
}

xmlFileReader.close();

这篇关于StaxItemReader 读取和打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆