我正在解析一个非常大的 xml 文件(> 2gb),信息按以下方式排列。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Document>
<CreditInfo>
<HeaderInfo>
<Name>Sample Value</Name>
</HeaderInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<PaymentInfo>
<value>100</value>
</PaymentInfo>
<OtherInfo>
<value>something</value>
</OtherInfo>
</CreditInfo>
</Document>
我不想将整个文档加载到内存中。我喜欢打一个电话来获取HeaderInfo
,然后一次获取PaymentInfo
一个,直到我到达文档的末尾。我在用spring-oxm
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
</dependency>
我当前的代码包含 Jaxb2Marshaller 配置
@Bean
Jaxb2Marshaller jaxb2Marshaller(){
Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
jaxb2Marshaller.setPackagesToScan(
"com.sample.model"
);
return jaxb2Marshaller;
}
我服务方法
@Service
public class ParsingService {
private final Jaxb2Marshaller jaxb2Marshaller;
public ParsingService(Jaxb2Marshaller jaxb2Marshaller) {
this.jaxb2Marshaller = jaxb2Marshaller;
}
public JAXBElement<Document> getDocument(InputStream fileInputStream){
return (JAXBElement<Document>) jaxb2Marshaller.unmarshal(new StreamSource(fileInputStream));
}
}
我试图添加其他方法getHeaderInfo
,PaymentInfo
但这不起作用。上述方法解组整个文档,我认为我不能将其存储在内存中。