Karl Ostner
Karl Ostner

Reputation: 1

libxml2: how to use DTD validation with xmlReader interface when DTD definition is in a memory buffer?

I'm using libxml2's xmlReader interface to process very large XML documents. All XML documents must correspond to a given syntax, which is currently contained in a DTD file, eg:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE myDoc SYSTEM "my.dtd">
...

The xmlReader code is like:

xmlTextReaderPtr reader = xmlReaderForFile(xmlFile, encoding, XML_PARSE_DTDATTR | XML_PARSE_DTDVALID);
while (xmlTextReaderRead(reader)) == 1)
  processNode(reader);

Things are working perfect, the XML document is streamed without significant memory consumption and DTD verification happens on the fly. Great!

My problem now: The requirement for my application changed from using the DTD definition in an external file to using a hard coded DTD definition in memory (which cannot be changed by a malicious application user).

How can I get the xmlReader interface to use a DTD definition from memory?

Using a DTD definition from memory is decribed here: Read in DTD from memory using libxml2 but the solution is not suitable for me since

I'm wondering if there is a way to let xmlReader use a DTD definition from memory, maybe somehow like:

static const char myDTD[] = "<DTD definition here>";

xmlTextReaderPtr reader = xmlReaderForFile(xmlFile, encoding, <whatever flags are required>);
        
xmlParserInputBufferPtr buf = xmlParserInputBufferCreateMem(myDTD, sizeof(myDTD), XML_CHAR_ENCODING_NONE);
xmlDtdPtr dtd = xmlIOParseDTD(NULL, buf, XML_CHAR_ENCODING_NONE);
        
< how to teach reader to use dtd ????? >
        
while (xmlTextReaderRead(reader)) == 1)
  processNode(reader);

Upvotes: 0

Views: 69

Answers (0)

Related Questions