【发布时间】:2021-12-27 15:56:49
【问题描述】:
我正在使用 BatchProcessing 过程解析 xml 文件,我只是想让该字段可选地从 StaxEventItemReader 读取,请检查它的以下代码行。
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;
Field 3如何为我的读者制作字段选项,请查看下面的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>
我希望字段 3 是可选的,因为它有时会出现。然而现在在 BatchProcessing 中,必须解析所有字段。我正在关注这个导师的 BatchProcessing 任务,请检查一次Click here
【问题讨论】:
-
请输入一些实际代码而不是一些愚蠢的版本。字段是可选的(除非有一个 xsd 认为不是这样,或者你通过 jaxb 注释强制它)。
-
可选是什么意思?如果该字段不存在,则 DTO 中的属性将为空
-
可选表示我不想解析这个字段。实际上 BatchProcessor 需要所有文件来解析。有没有可用的房产,请帮我看看
标签: java spring spring-boot spring-batch batch-processing