使用Spring批处理基于属性值解析xml片段

时间:2017-03-01 13:49:45

标签: java xml-parsing spring-batch

我想基于属性值解析xml,使用下面的弹簧批处理是XML供参考。

<?xml version="1.0" encoding="UTF-8"?>
<customerInfo>
<cutommer dept="IT">
<param value="Jane" name="first-name"/> 
<param value="Doe" name="last-name"/>
<param value="17 Streets" name="address"/> 
<param value="1234567" name="phone-number"/>
</customer>
<cutommer dept="ES">
<param value="Jane" name="first-name"/> 
<param value="Doe" name="last-name"/>
<param value="17 Streets" name="address"/> 
<param value="1234567" name="phone-number"/>
</customer>
</customerInfo>

基于上面的xml想要只解析dept属性值为“IT”的Customer标签。任何帮助都是适用的

更新1:

    @Configuration
@EnableBatchProcessing
public class ControllerInfoParser_Config extends DefaultBatchConfigurer {

    @Autowired
    private JobBuilderFactory jobs;
    @Autowired
    private StepBuilderFactory steps;
    @Bean
    public Job parseComponentInfoXML(Step parseComponentInfo,Step partitionStep, CustomJobExecutionerListener customJobExecutionerListener)
            throws UnexpectedInputException, ParseException, Exception {

        return jobs.get("parseComponentInfoXML").listener(customJobExecutionerListener).start(parseComponentInfo)
                .next(partitionStep).build();


    }
    @Bean
    public Step parseComponentInfo(ItemReader<Customer> oneDeptITItemReader) throws UnexpectedInputException, ParseException, Exception {

        return steps.get("parseComponentInfo").<Customer, Customer> chunk(1)
                .reader(componentInfoReader()).reader(oneDeptITItemReader).processor(componentInfoProcessor())
                .writer(componentInfoWriter()).build();
    }

    @Bean
    public ItemReader<Customer> componentInfoReader() throws UnexpectedInputException, ParseException, Exception {

       //OneDeptITItemReader <Customer> reader1 = new OneDeptITItemReader<Customer>();
        StaxEventItemReader<Customer> reader = new StaxEventItemReader<Customer>();
        reader.setResource(new ClassPathResource("xml//customer.xml"));
        reader.setFragmentRootElementName("customer");

        Jaxb2Marshaller marshaller = new org.springframework.oxm.jaxb.Jaxb2Marshaller();
        marshaller.setClassesToBeBound(Customer.class);

      //  marshaller.setSchema(new ClassPathResource("xml//company.xsd"));

        reader.setUnmarshaller(marshaller);

        return reader;
    }

    @Bean
    public ItemReader<Customer> oneDeptITItemReader(ItemReader<Customer> ir) {
        OneDeptITItemReader<Customer> odIR = new OneDeptITItemReader<Customer>();
        odIR.setDelegate(ir);
        return odIR;
    }
    @Bean
    public ItemProcessor<Customer, Customer> componentInfoProcessor() {

        return new CustomerProcessor();
    }

     @Bean
    public ItemWriter<Object>  componentInfoWriter() {

        return new SqlWritter();
    }
}

public class OneDeptITItemReader <T>  implements  ItemReader <Customer>{

    ItemReader<Customer> delegate;

      public ItemReader<Customer> getDelegate() {
        return delegate;
    }

    public void setDelegate(ItemReader<Customer> delegate) {
        this.delegate = delegate;
    }

    @Override
    public Customer read() {
        boolean read = true;
        Customer item = null;
        while(read)  {
          try {
            item = delegate.read();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            read =false;
        }
         read = !"IT".equals(item.getDept());
        }
        return item;
      }

}

2 个答案:

答案 0 :(得分:0)

停止阅读的方法是从ItemReader.read()返回class OneDeptITItemReader implements ItemReader<Customer> { ItemReader<Customer> delegate; @Override public Customer read() { boolean read = true; while(read) { Customer item = delegate.read(); read = read != null && !"IT".equals(item.getDept()); } return item; } } 写一个自定义的ItemReader委托,并在&#34; IT&#34;之后停止阅读。找到了部门。

RealmClass annotation does not support nested classes

使用委托时,您必须将委派的读者注册为流,以便让SB管理其生命周期。 见Request Body

答案 1 :(得分:0)

以下片段为我工作。

停止读取的方法是从ItemReader.read()返回null。 编写自定义ItemReader委托,并在找到“IT”部门后停止阅读。

class OneDeptITItemReader implements ItemReader<Customer> {
StaxEventItemReader<Customer> delegate;

public void setDelegate(StaxEventItemReader<Customer> delegate) {
        this.delegate = delegate;
    }

  @Override
  public Customer read() {
    boolean read = true;
   delegate.open(new ExecutionContext ());
   Customer item = null;
    while(read)  {
     item = delegate.read();
     read = item != null && !"IT".equals(item.getDept());
    }
    return item;
  }
}