Pregunta

I have a char string of a dtd file, and I need an xmlDtdPtr to pass to xmlValidateDtd along with my xml doc.

With the dtd as a file in the current directory I do this:

xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
xmlDocPtr doc = xmlCtxtReadFile(ctxt, filename, NULL,0);
xmlDtdPtr dtd = xmlParseDTD(NULL,(const xmlChar*)dtd_filename);
xmlValidateDtd(&(ctxt->vctxt),doc,dtd);

I tried this:

XmlDocPtr dtd_doc = xmlReadMemory(dtd_string,sizeof(dtd_string),"blah.dtd",NULL,XML_PARSE_DTDLOAD);
xmlDtdPtr dtd = xmlNewDtd(dtd_doc,NULL,NULL,NULL);

But I get parser errors complaining <!ELEMENT is an invalid element name. I can't find any good examples or documentation on how to properly parse in a DTD from memory.

¿Fue útil?

Solución

You have to use xmlIOParseDTD with an xmlParserInputBuffer:

xmlParserInputBufferPtr buf = xmlParserInputBufferCreateMem(string, size, XML_CHAR_ENCODING_NONE);
xmlDtdPtr dtd = xmlIOParseDTD(NULL, buf, XML_CHAR_ENCODING_NONE);
xmlFreeParserInputBuffer(buf);

Replace XML_CHAR_ENCODING_NONE with the appropriate encoding if your DTD doesn't contain a text declaration.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top