netvl / xml-rs

An XML library in Rust
MIT License
461 stars 113 forks source link

[Question] How to implement streaming parsing? #216

Closed derchr closed 2 years ago

derchr commented 2 years ago

Hello, I'm currently working on a very memory restricted platform (embedded system) and need to parse XML. I cannot store the whole XML file at once in memory as it is bigger than my available RAM. My idea was to download a part of the XML file into a buffer, parse it on the fly, only extract the information I need, and download the next part of the file.

For that I need to access the buffer that was passed to the EventReader. But currently I can't find a way to make this work with the borrow checker, as I am always trying to modify the buffer (to load the new data into it) while a shared reference is held by the EventParser. As we know, modifying data that somebody helds a shared reference to, is a big no no in Rust.

I saw the streaming.rs test in the repository but I'm not sure if I can apply it to my use case. How can something like this be implemented with the crate? Thank you!

derchr commented 2 years ago

It turns out, I had some misunderstandings on how the reader trait works.

All I had to do was passing my reader (or BufReader) to the EventReader and now I am streaming.