Spring Batch - StaxItemReader Jaxb读取多个嵌套的xml节点

时间:2017-03-20 12:23:28

标签: java xml jaxb spring-batch

Iam尝试使用StaxEventItemReader和Jaxb2Marshaller读取嵌套了xml节点的xml文件。我做错了什么导致异常的地方。

将在此处提供示例XML文件

File.xml

    <File>
        <FileDate>05/28/2016</FileDate>
        <RecordCount>75</RecordCount>
        <Transaction>
           <RecordID>1</RecordID>
           <MemberDetails>
               <Id>A2334549</Id>
               <MemberDemoData>
                   <SubID>89890734548557</SubID>
                   <MemberSuffix>01</MemberSuffix>
                   <SSN>XXXXX</SSN>
                   <CategoryCode>B</CategoryCode>
                   <Gender>F</Gender>
                   <LastName>Naaz</LastName>
                   <FirstName>Affu</FirstName>
                   <MiddleInitial>H</MiddleInitial>
                   <BirthDate>04/02/1990</BirthDate>
                   <HICNumber>4567376739A</HICNumber>
                   <PhoneNumber>894567893</PhoneNumber>
                   <ProviderID>09678546</ProviderID>
                   <PCPOverride>N</PCPOverride>
                   <Address>
                       <AddressType>HOME</AddressType>
                       <Address1>2226 Indira nagar RD</Address1>
                       <City>Noida</City>
                       <State>Delhi</State>
                       <Zip>525001</Zip>
                       <County>India</County>
                   </Address>
                   <Address>
                       <AddressType>MAIL</AddressType>
                       <Address1>2226 Indira nagar RD</Address1>
                       <City>Noida</City>
                       <State>Delhi</State>
                       <Zip>525001</Zip>
                       <County>India</County>
                   </Address>
                   <AgentInfo>
                       <MailAgentNo>40</MailAgentNo>
                       <AgentNo>67894</AgentNo>
                   </AgentInfo>
              </MemberDemoData>
              <Package>
                 <Plan>Yu8883</Plan>
                 <PlanId>009</PlanId>
                 <SegmentId>104</SegmentId>
                 <Group>000987666</Group>
                 <SubGroup>B004</SubGroup>
                 <Class>0004</Class>
                 <PartD>
                     <BIN>789034</BIN>
                     <PCN>*******</RxPCN>
                 </PartD>
                 <EffectiveDate>09/01/2016/01/2016</EffectiveDate>
                 <TerminationDate>12/31/2016</TerminationDate>
             </Package>
             <LICSInfo>
                 <SubsidyLevel>100</SubsidyLevel>
                 <CopayCategory>1</CopayCategory>
                 <SubsidyLevelStartDate>08/11/2016</SubsidyLevelStartDate>
             </LICSInfo>
             <MedihealthData>
                    <PartA>
                       <EffectiveDate>02/01/2008</EffectiveDate>
                    </PartA>
                    <PartB>
                       <EffectiveDate>02/01/2008</EffectiveDate>
                    </PartB>
                    <ESignDate>
                       <EffectiveDate>08/16/2016</EffectiveDate>
                    </ESignDate>
            <ElectionPeriod>S</ElectionPeriod>
                    <EnrollmentDate>
                       <EffectiveDate>08/01/2016</EffectiveDate>
                    </EnrollmentDate>
               </MedihealthData>
               <BillingData>
                    <PremiumWithHold>D</PremiumWithHold>
               </BillingData>
           </MemberDetails>
        </Transaction>
        <Transaction>
         -------
        </Transaction>
        <Transaction>
         ------
        </Transaction>
        <Transaction>
         -------
        </Transaction>
    </File>

拥有Jaxb类 - &gt;文件,事务,MemberDetails,MemberDemoData,Address,AgentInfo,Package,PartD,LICSInfo,MedihealthData,PartA,PartB,ESignDate,EnrollmentDate,BillingData

样本类

  1. File.java

           @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "fileDate",
        "recordCount",
        "Transaction"
    })
    @XmlRootElement(name = "File")
    class File{
        @XmlElement(name = "FileDate", required = true)
        protected String fileDate;
        @XmlElement(name = "RecordCount", required = true)
        protected String recordCount;
        @XmlElement(name = "Transaction")
        protected List<Transaction> transaction;
    
    //setters and getters
    }
    
  2. 其中File.java是所有xml元素的根类

    文件中的重复元素是交易

    1. Transaction.java

      @XmlAccessorType(XmlAccessType.FIELD)
      @XmlType(name = "", propOrder = {
          "recordID",
          "memberDetails"
      })
      @XmlRootElement(name = "MPTransaction")
      
      public class Transaction {
      
          @XmlElement(name = "RecordID", required = true)
          protected String recordID;
          @XmlElement(name = "MemberDetails", required = true)
          protected MemberDetails memberDetails;
      // setter and getter
      
      }
      
    2. 现在我在SpringBatch中编写了ItemReader方法

      读取File.xml的ItemReader方法

          @Bean
          @StepScope
          public ItemReader<File> fileXmlReader(@Value("#{stepExecution}") StepExecution stepExecution) {
      
                  StaxEventItemReader<File> xmlFileReader = new StaxEventItemReader<>();
      
                  xmlFileReader.setResource(new ClassPathResource("/File.xml"));
      
                  xmlFileReader.setFragmentRootElementNames(new String[] {"Transaction" , "MemberDetails" ,
                          "MemberDemoData", "Address" ,"AgentInfo","Package" , "PartD" ,"LICSInfo" , "MedihealthData",
                          "PartA", "PartB","ESignDate","EnrollmentDate","BillingData"} );
      
                  Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
                  marshaller.setClassesToBeBound(Transaction.class,MemberDetails.class,
                          MemberDemoData.class,Address.class,AgentInfo.class,Package.class,
                          PartD.class,LICSInfo.class,MedihealthData.class,PartA.class,PartB.class,
                          ESignDate.class,EnrollmentDate.class,BillingData.class);
      
                  xmlFileReader.setUnmarshaller(marshaller);
      
                  return xmlFileReader;
      
          }
      

      如果我运行我的Batch iam获得以下异常。请帮我确定哪里出错了?

          java.lang.NullPointerException
              at org.springframework.batch.item.xml.StaxEventItemReader.moveCursorToNextFragment(StaxEventItemReader.java:151)
              at org.springframework.batch.item.xml.StaxEventItemReader.doRead(StaxEventItemReader.java:228)
              at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.read(AbstractItemCountingItemStreamItemReader.java:88)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
              at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
              at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
              at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
              at com.sun.proxy.$Proxy34.read(Unknown Source)
              at org.springframework.batch.core.step.item.SimpleChunkProvider.doRead(SimpleChunkProvider.java:91)
              at org.springframework.batch.core.step.item.SimpleChunkProvider.read(SimpleChunkProvider.java:155)
              at org.springframework.batch.core.step.item.SimpleChunkProvider$1.doInIteration(SimpleChunkProvider.java:114)
              at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
              at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
              at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
              at org.springframework.batch.core.step.item.SimpleChunkProvider.provide(SimpleChunkProvider.java:108)
              at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:69)
              at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
              at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
              at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
              at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
              at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
              at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
              at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
              at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
              at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
              at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
              at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
              at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:386)
              at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135)
              at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
              at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
              at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
              at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
              at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
              at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)
              at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
              at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
              at com.sun.proxy.$Proxy37.run(Unknown Source)
              at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:210)
              at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:227)
              at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:121)
              at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:115)
              at org.springframework.boot.SpringApplication.runCommandLineRunners(SpringApplication.java:672)
              at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:690)
              at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
              at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
              at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
              at com.bcn.medicareEnrollment.Runner.main(Runner.java)
      

      请帮我解决这个问题。我刚接触Spring批次。当我运行这个xml阅读器逻辑时,我得到了异常。

0 个答案:

没有答案