Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 919
Max Kielland Avatar asked May 27 '12 01:05

Max Kielland


2 Answers

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;
like image 150
Remy Lebeau Avatar answered Nov 09 '22 00:11

Remy Lebeau


Since the workaround with the global variable MSXML6_ProhibitDTD is deprecated and I couldn't get it to work with XE5 either, here is another solution:

As stated in the documentation, there is this method to change the DOM property

Xml.Win.Msxmldom.MSXMLDOMDocumentFactory.AddDOMProperty

Unfortunately it's not so trivial to use this...

include the header for this namespace:

#include <Xml.Win.msxmldom.hpp>

Foo::Foo()
{
     //change the dom property in your constructor.
    ((TMSXMLDOMDocumentFactory*)Xml::Win::Msxmldom::MSXMLDOMDocumentFactory)->AddDOMProperty("ProhibitDTD", False, true);
}

and access this method. (The cast is necessary, because the MSXMLDOMDocumentFactory itself is inherited from a metaclass interface or so. I don't got the concept behind.)

inspired from a delphi blog: https://bobsotherblog.wordpress.com/2013/09/19/fixing-dtd-is-prohibited-error-in-delphi/

like image 22
Raphael Müller Avatar answered Nov 09 '22 00:11

Raphael Müller