0

I have a spring batch application that is meant to read an xml from an outside vendor and translate data from it into dat file. The problem is that the XML file has and XMLNS namespace url that is causing an Unmarshalling exception whenever I try to read the file. I originally had a function that would turn the xml into a string and remove it before changing it back and then running it through the reader but that used up a lot of memory and I started getting Java Heap Memory exceptions. Looking online I found articles about how to ignore the Namespaces but nothing for Spring Batch itemReaders.

I figured I would need to create either a custom StaxItemReader or a custom Jaxb2Marshaller but I can't figure out what functions of the original StaxItemReader or Jaxb2Marshaller I would need to Override. If anyone could help figure this out I would appreciate it.

    @Bean
    ItemReader<Object> xmlFileItemReader(){
      File inputFilePath = new File(fileInputPath);
      ArrayList<String> filePaths = new ArrayList<>(Arrays.asList(Objects.requireNonNull(inputFilePath.list())));
      Resource inputResource = new FileSystemResource(fileInputPath + "\\" + filePaths.get(0));

      Jaxb2Marshaller objectrMarshaller = new Jaxb2Marshaller();
      objectMarshaller.setClassesToBeBound(Object.class);

      StaxEventItemReader<Object> xmlFileReader = new StaxEventItemReader<>();
      xmlFileReader.setResource(inputResource); //editedInput
      xmlFileReader.setFragmentRootElementName("RElement");
      xmlFileReader.setUnmarshaller(objectMarshaller);

      return xmlFileReader;
}

This article was the most helpful but I couldn't really figure out how to translate it to my needs:

JAXB: How to ignore namespaces during unmarshalling

Karson074
  • 97
  • 1
  • 14

0 Answers0