nestable C/C++ XML parser?

Thomas B. Passin tpassin at
Wed Dec 8 13:47:34 GMT 1999

----- Original Message -----
From: Paul Miller <stele at>

> ...It [...] would be nicer to be able treat parsing of an element as an
> operation, so you can write code like this:
> Document::ParseDocument(XML_Input &in)
> {
> XML_ElementHandler handlers[] = {
> { "Object", ParseObject }
> { NULL }
> };
> in.Parse(handlers, this);
> }
> Docuement::ParseObject(XML_Element &element, void *userData)
> {
> Document *doc = (Document *)userData;
> Object *obj = new Object;
> obj->Parse(element);
> doc->AddObject(obj);
> }

> You see in ParseObject() that I can do everything I need to create a new
> object, parse it, and do something with it after I've parsed it. I can
> only do this if the parser lets me parse just a subtree and then stop
> (ie. it returns control back to me when it finds the </Object> token).
> --
> Paul Miller - stele at

You can see the difficulty - if you send a fragment to a parser it's not a
valid xml document (so the parser can't work with it).  You could start
building a subtree when you get to the point of interest, using DOM calls,
but you keep saying you don't want to deal with DOM.

Where you are being unclear is when you say "parse just a subtree".  It is
unclear whether you think you need to get (or build) an actual tree
structure, or whether the expression is just a shorthand for indicating a
place in the document.  It is also unclear when you say that, because how do
you know that you are at the right starting place in the document?  I assume
that you have been parsing from the start of the document to get to the
point of interest.  Then you say you want to start parsing at that point.
See why it's confusing?

If you just want to know the names of the elements in the fragment, just
keep a state variable.  I know you said it's too much machinery, but maybe
there is a way it wouldn't be.

Alternatively, there are other tree builders that are simpler than DOM.
Look at Sean McGrath's xml tree code in (I think) "XML By Example) for one
example.  Of course it depends on the complexity of what you are doing.

All in all, I still think that a preprocessing pass to extract the fragments
you want to look at, as I mentioned in my previous post, is the way to go.

Tom Passin

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at
Archived as: and on CD-ROM/ISBN 981-02-3594-1
To unsubscribe, mailto:majordomo at the following message;
unsubscribe xml-dev
To subscribe to the digests, mailto:majordomo at the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at

More information about the Xml-dev mailing list