Can JAXB parse large XML files in chunks

前端 未结 4 1352
终归单人心
终归单人心 2020-11-29 03:26

I need to parse potentially large XML files, of which the schema is already provided to me in several XSD files, so XML binding is highly favored. I\'d like to know if I can

4条回答
  •  失恋的感觉
    2020-11-29 04:03

    I wrote a small library (available on Maven Central) to help to read big XML files and process them by chunks. Please note it can only be applied for files with a unique container having a list of data (even from different types). In other words, your file has to follow the structure:

    
       ...
       ...
       ...
       ...
    
    

    Here is an example where Type1, Type2, ... are the JAXB representation of the repeating data in the file:

    try (StreamingUnmarshaller unmarshaller = new StreamingUnmarshaller(Type1.class, Type2.class, ...)) {
        unmarshaller.open(new FileInputStream(fileName));
        unmarshaller.iterate((type, element) -> doWhatYouWant(element));
    }
    

    You can find more information with detailed examples on the GitHub page of the library.

提交回复
热议问题