Julie Lehninger
Julie Lehninger

Reputation: 51

Parse fixed-width file with RowProcessor (Univocity)

I'm dealing with a fixed-width file with 3 different types of bean :

I've tried many options of Univocity but the problem is that the processor.getBeans() methods always return an empty list for each type of bean after the parsing step.

Can anybody help me ?

        FixedWidthParserSettings settings = new FixedWidthParserSettings();
        settings.setAutoConfigurationEnabled(true);
        settings.setHeaderExtractionEnabled(false);
        settings.setRecordEndsOnNewline(true);
        settings.trimValues(false);
        
        FixedWidthFields header = FixedWidthFields.forParsing(Header0256Q.class);
        FixedWidthFields payment = FixedWidthFields.forParsing(Payment0768T.class);
        FixedWidthFields footer = FixedWidthFields.forParsing(Footer0256Z.class);
        

Lookahead

        settings.addFormatForLookahead("0256Q", header);
        settings.addFormatForLookahead("0768T", payment);
        settings.addFormatForLookahead("0256Z", footer);
        

Create a processor per type of record

        BeanListProcessor<Header0256Q> headerProcessor = new BeanListProcessor<Header0256Q>(Header0256Q.class);
        BeanListProcessor<Payment0768T> paymentProcessor = new BeanListProcessor<Payment0768T>(Payment0768T.class);
        BeanListProcessor<Footer0256Z> footerProcessor = new BeanListProcessor<Footer0256Z>(Footer0256Z.class);
        

InputValueSwitch

        InputValueSwitch inputValueSwitch = new InputValueSwitch();
       
        inputValueSwitch.addSwitchForValue("0256Q", headerProcessor);
        inputValueSwitch.addSwitchForValue("0768T", paymentProcessor);
        inputValueSwitch.addSwitchForValue("0256Z", footerProcessor);
       
        settings.setProcessor(inputValueSwitch);

Parsing

        FixedWidthParser parser = new FixedWidthParser(settings);
        parser.parse(lFile);

Get beans

        List<Header0256Q> headerRecords = headerProcessor.getBeans();
        List<Payment0768T> paymentRecords = paymentProcessor.getBeans();
        List<Footer0256Z> footerRecords = footerProcessor.getBeans();
        

Map the file with returned beans

        Dtazv dtazv = new Dtazv();
        dtazv.setHeaderRecord(headerRecords.get(0));
        dtazv.setPaymentRecords(paymentRecords);
        dtazv.setFooterRecord(footerRecords.get(0));

Upvotes: 1

Views: 494

Answers (0)

Related Questions