Best XML Library
Adam D. Ruppe
destructionator at gmail.com
Tue May 28 16:21:39 PDT 2013
On Friday, 24 May 2013 at 14:18:38 UTC, Adam D. Ruppe wrote:
> I don't think it would work best for xmpp though because dom.d
> needs the entire file before it can parse, and xmpp is a stream
> of data.
I just decided to write a quick stream like thing, to see if I
can, and it actually kinda works.
My dom.d is still NOT an ideal choice for xmpp, I was just
wondering if I could do this easily and figured I'd share the
results.
dom.d
https://github.com/adamdruppe/misc-stuff-including-D-programming-language-web-stuff/blob/master/dom.d
also needs characterencodings.d from that same github.
===
import arsd.dom;
import std.stdio;
void main() {
auto document = new Document();
document.eventObservers ~= delegate(DomMutationEvent event) {
if(event.operation == DomMutationOperations.appendChild) {
auto element = event.related;
if(element.tagName == "cool")
writeln("GOT: " ~ element.toString());
}
};
document.parseStream(new Utf8Stream(delegate string() {
return readln();
}, delegate bool() {
return stdin.isOpen;
}), true, true);
}
===
$ dmd streamtest.d dom.d characterencodings.d
$ ./streamtest # I type the tags there into the console
<test>
<cool>sweet</cool>
GOT: <cool>sweet</cool> # note how this showed up instantly
</test>
The parse function expected a complete string, and I didn't
signficantly modify it. Instead I created a class Utf8Stream that
overloads enough operators that it can pretend to be a string,
but one that can fetch more data when its length is checked and
it is close to the last index fetched (the parse function checks
data.length often to avoid going out of bounds, and it fetches
chars by opIndex one at a time, so it works here, but wouldn't
likely work elsewhere. Heck it might even break with non-ascii
input, I haven't tried that yet)
So yeah pretty hacky, but since it internally builds the tree and
fires off these mutation event things, you can hook into that and
react as it is built, accomplishing the task at hand, more or
less.
But since it is still building a big old dom and appending
strings internally, it will be a bit of a memory hog as time goes
on, and performance may not be great.
It also works better in strict mode (The true, true on the parse
function argument list does this) than garbage mode when
streaming.
Like I said, dom.d still isn't my first choice for xmpp or any
other enormous xml parsing task, but hey it kinda works and is
fairly convenient so if this looks useful to you, have fun!
More information about the Digitalmars-d
mailing list