【问题标题】:How to use StaxEventItemReader in Spring Batch for complex xml如何在 Spring Batch 中使用 StaxEventItemReader 处理复杂的 xml
【发布时间】:2017-03-22 05:15:46
【问题描述】:

请帮助我编写 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>

我为 File、Transaction、MemberDetails、MemberDemoData、Address、AgentInfo、Package、PartD、LICSInfo、MedihealthData、PartA、PartB、ESignDate、EnrollmentDate、BillingData 生成了 jaxb 类

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

    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请帮我填写ItemReader方法“?”使用正确的类名以获得正确的输出

标签: java spring jaxb spring-batch xmlreader


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    相关资源
    最近更新 更多