nestable C/C++ XML parser?

Paul Miller stele at fxtech.com
Tue Dec 7 20:45:29 GMT 1999


> Ignore my response on xml-dev: I incorrectly guessed what you wanted to
> do.  Let me try again.  This time it looks like what you want to do is
> process Foo and its contents with a different set of handlers than the
> rest of the document.  If that's the case, have your "standard"
> StartElement handler set new handlers when it encounters a Foo and have
> the new EndElement handler set the handlers back to the "standard" ones
> when it encounters the end of a Foo.  If necessary, maintain a stack of
> pointers to handlers.

This is a good idea on the surface, and where I started down in my
implementation when I hit a snag. This provides too much housekeeping,
and too many functions if you want to do something special when the
element is finished (such as add the just-parsed object to a list). It
would be nicer to be able treat parsing of an element as an atomic
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);
}

Object::Parse(XML_Element &element)
{
	XML_ElementHandler handlers[] = {
		... object-specific element handlers ...
	};
	// parse just the object subtree to the </Object> token
	element.Parse(handlers, this);
}

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 fxtech.com

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





More information about the Xml-dev mailing list