Search code examples
javaspringspring-bootspring-batchbatch-processing

Batch processing of the XML file with optional values?


I am parsing the xml file using the BatchProcessing process, I just want to make the field optional to read from the StaxEventItemReader, please check the below lines of code of it.

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

    reader.setResource(new PathResource(filePath));

    reader.setFragmentRootElementName("MY_LIST");


    Map<String, Class<?>> aliases = new HashMap<>();
    aliases.put("MY_LIST", DataWrapper.class);

    XStreamMarshaller xStreamMarshaller = new XStreamMarshaller();

    xStreamMarshaller.setAliases(aliases);

    xStreamMarshaller.getXStream().addPermission(AnyTypePermission.ANY);


    reader.setUnmarshaller(xStreamMarshaller);


    SynchronizedItemStreamReader<DataWrapper> synchronizedItemStreamReader = new SynchronizedItemStreamReader<>();
    synchronizedItemStreamReader.setDelegate(reader);

    return synchronizedItemStreamReader;

How to make the field option for the my reader for the Field 3, please check the below XMl

<?xml version="1.0" encoding="utf-8"?>
<Fields xmlns:xsi="http://www.wwww.org/2001/XMLSchema-instance" >
  <FileCreationDate>2021-04-30T00:15:55.0725852-07:00</FileCreationDate>
  <MY_LIST>
    <Field 1>Fileld 1 data</Field 1>
    <Field 2>Fileld 2 data</Field 2>
    <Field 3>Fileld 3 data</Field 3> /// I WANT TO PUT IT AS THE OPTIONAL WHILE READING
  </MY_LIST>
</Fields>

I want the Field 3 optional because it comes sometime. Yet now in BatchProcessing it is mandatory to parse all field.I am following this tutor for my BatchProcessing task , please ceck it once Click here


Solution

  • Thanks for all suggestion , I have fixed the solution by the ignoreUnknownElements(); of the XStream class.

    Since XStream 1.4.5 durring marshaller declaration it's enough to use ignoreEnknownElements() method:

    XStreamMarshaller marshaller = new XStreamMarshaller();
    marshaller.getXStream().ignoreUnknownElements();