C++ Builder XE2, TXMLDocument 'DTD is prohibited'
Asked Answered
R

3

6

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

Rettke answered 27/5, 2012 at 1:30 Comment(0)
H
5

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;
Hazeghi answered 28/5, 2012 at 1:3 Comment(3)
Thank you it works like a charm! Strange that this very piece of information should be so hard to find... Why is IXMLDocument better than TXMLDocument? As I understood, IXMLDocument is a part of TXMLDocument.Rettke
TXMLDocument implements the IXMLDocument interface, so it has the same functionality. However, if you dynamically instantiate TXMLDocument with a NULL Owner (which you should do when creating short-lived XML objects), it acts as a reference-counted object. This is documented behavior. It is not safe to assign a dynamic instance of TXMLDocument to a TXMLDocument* variable unless it has an Owner assigned. Otherwise, you have to assign it to a _di_IXMLDocument variable instead to maintain the reference count correctly.Hazeghi
Thank you for the explanation. In my case the dynamic instance of TXMLDocument* always have an owner and lives through the whole application. I have earlier project code working with TXMLDocument, so I use it out of convenience to reuse the old code.Rettke
K
1

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/

Kraken answered 24/4, 2015 at 7:22 Comment(0)
M
0

You need to copy MSXMLDOM.pas into your project folder, and modify it in order to fix this issue.

Change the implementation of function TMSDOMDocument.GetMSDocument to the following, and then rebuild your project.

Note you have to use IXMLDOMDocument2.setProperty instead of accessing ProhibitDTD directly, as IXMLDOMDocument2 doesn't publish ProhibitDTD.

function TMSDOMDocument.GetMSDocument: IXMLDOMDocument;
var
  Doc2: IXMLDOMDocument2;
begin
  Result := MSNode as IXMLDOMDocument;
  if Supports(Result, IXMLDOMDocument2, Doc2) then
      Doc2.setProperty('ProhibitDTD', False);
end;

Note that this will only work if you're not building with runtime packages!

This solution is from an Embarcadero forums post made by a member of TeamB; I remembered reading it, and found it in a search of those forums via CodeNewsFast - search functionality at the EMBT forums hasn't ever worked well, and a recent rebuild or reindex or something has made it even worse than before. :-)

Macadam answered 27/5, 2012 at 3:52 Comment(2)
I am the TeamB member who discussed this workaround in the EMBT forums. This workaround is only needed in XE and earlier. XE2 introduced a native solution to this exact problem. See my answer.Hazeghi
Yeah, @Remy. I know. That's why I posted the link to the search that I got it from; so people would know I wasn't taking credit for coming up with the solution. :-)Macadam

© 2022 - 2024 — McMap. All rights reserved.