-
I'm parsing RSS, but they get huge at times. I've seen this library can lazily parse XML as well, which, I would think, would have less memory and CPU footprint. RSS has multiple
So, my reasoning is, when I hit an So far, my humble implementation (lol) is this:
...which I'm not quite sure if is true. I'm assuming I'm hitting an After this point, I'm not sure how I can approach. How can I convert this to Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You assumptions are good, and the code would give you the var started = false;
for (final event in parseEvents(input)) {
if (event is XmlStartElementEvent && event.name == 'item') {
started = true;
} else if (event is XmlEndElementEvent && event.name == 'item') {
started = false;
} else if (started) {
print(event); // an event that is part of `<item>...</item>`
}
} Since this is such a common operation there are helpers that do exactly that. However, they work on final source = Stream.value(input); // could be an async download or file-read, see documentation for examples
await source.toXmlEvents()
.normalizeEvents()
.selectSubtreeEvents((event) => event.name == 'item') // only select the events within <item>
.toXmlNodes() // convert the remaining events to XmlNode
.expand((nodes) => nodes) // convert `Stream<List<XmlNode>>` to `Stream<XmlNode>`
.forEach((node) => print(node)); // print each `XmlNode` Documentation is here, the |
Beta Was this translation helpful? Give feedback.
You assumptions are good, and the code would give you the
<item>
event of the input, but nothing else. To make it work, you would need to manually iterate over the events, detect the start<item>
, then start to buildXmlNode
until you reach the next</item>
. Since you need to remember state during the iteration, this unfortunately doesn't work that well with the built-in iterator methods such aswhere
, but you can get this to work something along the lines of: