vclc++builder-xe2dtdtxmldocumentxml

C++ Builder XE2, TXMLDocument 'DTD is prohibited'


When I try to read an XML document (eagle file) with an DTD I get the error:

Project xx raised exception class EDOMParserError with message 'DTD is prohibited'

The XML header looks like this:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE eagle SYSTEM "eagle.dtd">

If I remove the second line...

<!DOCTYPE eagle SYSTEM "eagle.dtd">

...everything works fine.

After some googling it seems like the MSXML parser have an option called ´prohibitDTD´ set to true by default (in earlier versions it was false).

However it seems not possible to set this option to false from the TXMLDocument class. One solution seems to be a recompile of the .pas library or to create the interface on my own with CoCreateInstance().

All examples I have seen out there are in Delphi and I'm having dificulties to trasnlate these to C++ Builder.

Does anyone know how to read a DTD XML document with C++ Builder XE2?

My example code...

#include <xmldoc.hpp>

_di_IXMLNode XMLObject;

TXMLDocument *XMLDocument = new TXMLDocument(this);
XMLDocument->LoadFromFile(fileName); // <----- Exception EDOMParserError
XMLObject = XMLDocument->DocumentElement;

Thank you...


Solution

  • XE2 introduced a native solution to this very problem: there is a global bool variable named MSXML6_ProhibitDTD declared in Xml.Win.msxmldom.hpp. You can set it to false before loading data into TXMLDocument:

    #include <xmldoc.hpp>
    #include <msxmldom.hpp>
    
    MSXML6_ProhibitDTD = false;
    TXMLDocument *XMLDocument = new TXMLDocument(this):
    XMLDocument->LoadFromFile(fileName);
    _di_IXMLNode XMLObject = XMLDocument->DocumentElement;
    

    On a side note: it is generally not a good idea to create TXMLDocument instances dynamically like this. It is better to use the IXMLDocument interface instead:

    #include <xmldoc.hpp>
    #include <msxmldom.hpp>
    
    MSXML6_ProhibitDTD = false;
    _di_IXMLDocument XMLDocument = LoadXMLDocument(fileName);
    _di_IXMLNode XMLObject = XMLDocument->DocumentElement;