Altova Mailing List Archives


Re: How to add DTD to DOM document?

From: Martin Honnen <mahotrash@-----.-->
To: NULL
Date: 8/5/2004 3:56:00 PM

Ken Bass wrote:

> I have an application that generates XML documents dynamically. I
> would like to add a doctype (<!DOCTYPE ....>) to the DOMDocument, in
> particular, to specify the DTD.
> 
> Is there a way to do this?
> 
> I am using MSXML4.dll with C++/Smart pointers.

When you start creating your XML document you can use the loadXML method 
to initialize the document with a DOCTYPE declaration and a root 
element, I can't give you the C++ syntax but JScript is

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.4.0');
xmlDocument.async = false;
xmlDocument.validateOnParse = false;

var xmlSource = "";
xmlSource += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\r";
xmlSource += "<!DOCTYPE gods SYSTEM \"test20040719.dtd\">\r\n";
xmlSource += "<gods />";

var loaded = xmlDocument.loadXML(xmlSource);

if (loaded) {
   var el = xmlDocument.createElement("god");
   xmlDocument.documentElement.appendChild(el);
   var parseError = xmlDocument.validate();
   if (parseError.errorCode != 0) {
     alert(parseError.reason);
   }
}


where the inital XML source text with the DOCTYPE declaration is 
constructed from a string, then you can use the DOM to add elements and 
you can validate when needed as shown in the example.

-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.