如何在Spring Batch中使用StaxEventItemReader来处理复杂的xml

时间:2017-03-22 05:15:46

标签: java spring jaxb spring-batch xmlreader

请帮我写ItemReader()

    <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>

我为文件,事务,MemberDetails,MemberDemoData,Address,AgentInfo,Package,PartD,LICSInfo,MedihealthData,PartA,PartB,ESignDate,EnrollmentDate,BillingData

生成了jaxb类

现在在下面的ItemReader方法中代替&#34;?&#34;我应该使用什么类名。请帮我。

    public ItemReader<?> fileXmlReader(@Value("#{stepExecution}") StepExecution stepExecution) {

            StaxEventItemReader<?> xmlFileReader = new StaxEventItemReader<>();

            xmlFileReader.setResource(new ClassPathResource("/File.xml"));

            xmlFileReader.setFragmentRootElementNames( new String[] {?}); (or )
            xmlFileReader.setFragmentRootElementName(?)

            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            marshaller.setClassesToBeBound(?);

            xmlFileReader.setUnmarshaller(marshaller);

            return xmlFileReader;

    }

1 个答案:

答案 0 :(得分:-1)

@Bean
public StaxEventItemReader<Entity> reader() {

    XStreamMarshaller unmarshaller = new XStreamMarshaller();

    Map<String, Class> aliases = new HashMap<>();
    aliases.put("File", Entity.class);

    unmarshaller.setAliases(aliases);

    StaxEventItemReader<Customer> reader = new StaxEventItemReader<>();

    reader.setResource(new ClassPathResource("/data/file.xml"));
    reader.setFragmentRootElementName("file");
    reader.setUnmarshaller(unmarshaller);


    return reader;
}