接下来,根据您要直接测试的读写器,您必须调用 reader.open(new ExecutionContext()).之后,您可以调用 read() 方法,该方法应该会逐项返回.main(){StaxEventItemReaderxmlFileReader = 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 fileSo 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 necessaryxmlFileReader.open(new ExecutionContext()); // does some initialisation, so you need to call itStuden student = null;while(student = xmlFileReader.read() != null) { System.out.println(student...); }xmlFileReader.close(); 这篇关于StaxItemReader 读取和打印值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-12 12:27