[xml] Resolving entity references from a DTD using SAX parser


Please could you help me get libxml2 to resolve entity references such as &foo; which are defined in a DTD declared in the XML document?

I am using the SAX parser (I know about the warning on http://xmlsoft.org/entities.html, but we really have to use SAX for our purposes), declaring element handlers and character handlers and then calling XMLParseChunk. I originally assumed the parser would automatically read in the DTD when it found the declaration, and then resolve the custom-defined entity references as it found them, like it does with the predefined ones such as & but this seems not to be the case.

I think I have to declare a reference-handling function as SAXHandler.reference, and then in that function, which gets called when an entity reference is found, call xmlGetEntityFromTable to look up what that entity reference resolves to. But how do I populate the table using the entity references defined in the DTD? The functions like xmlAddDtdEntity and xmlAddDocEntity seem to require an xmlDocPtr, which I thought only existed when doing DOM parsing. So do I need to use xmlNewEntity instead, which does the same thing without needing an xmlDocPtr? Also, xmlGetEntityFromTable isn't exposed by the API. It's called from xmlGetDocEntity, xmlGetDtdEntity and xmlGetParameterEntity, but they all require an xmlDocPtr too. May I change the API so that xmlGetEntityFromTable is exposed, or is there another way of doing it?

Also, how do I get the filename of the DTD? Is there a function for that, or do I need to manually parse the XML document looking for the declaration? Then when I have the DTD, I need to parse it. Calling xmlParseDTD with the filename as the SystemID argument seems to work; then can I use the "elements" field of the xmlDtdPtr it returns as the table I need to refer to in xmlGetEntityFromTable (and so not need xmlAdd*Entry)?

Thanks very much,

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]