How to set the document type XMLDOMDocument?

How can I set the doctype object?

For example, I try:

 IXMLDOMDocument60 doc = new DOMDocument60(); doc.doctype.name = "html"; 

except IXMLDOMDocumentType.name read-only :

 IXMLDOMDocumentType = interface(IXMLDOMNode) { ['{2933BF8B-7B36-11D2-B20E-00C04F983E60}'] string Get_name(); ... property String name read Get_name; } 

and IXMLDOMDocument60.doctype read-only :

 IXMLDOMDocument = interface(IXMLDOMNode) { ['{2933BF81-7B36-11D2-B20E-00C04F983E60}'] IXMLDOMDocumentType Get_doctype(); ... property IXMLDOMDocumentType doctype read Get_doctype; } 

So how can I set the doctype an XML document?


Bonus question: How to create a DOMDocument60 object with the specified doctype ?


Note: You do not see the mention of XSLT because they are not. I am creating an HTML DOM tree in MSXML.

+4
source share
1 answer

For performance and security reasons , Microsoft does not usually allow <!DOCTYPE> (aka Document Type Definition ). Because of this, you must use the loadXML method to set <!DOCTYPE> . Therefore, it cannot be installed after creating or importing a document.

Also, because of the default security settings in MSXML6, you usually cannot import XML with <!DOCTYPE> . Therefore, you must disable the ProhibitDTD setting of the object.

Edit: You should be aware that HTML5 is not XML . In addition, <!DOCTYPE> is considered optional for XHTML5 .

First, start with the desired exit.

 <!DOCTYPE html> <html /> 

Based on the syntax, I assume that you are using C # and have added a link to msxml6.dll . The following code will allow you to create these two processing instructions.

 MSXML2.DOMDocument60 doc = new MSXML2.DOMDocument60(); // Disable validation when importing the XML doc.validateOnParse = false; // Enable the ability to import XML that contains <!DOCTYPE> doc.setProperty("ProhibitDTD", false); // Perform the import doc.loadXML("<!DOCTYPE html><html />"); // Display the imported XML Console.WriteLine(doc.xml); 

Here is also a copy of the code written in VBScript.

 Set doc = CreateObject("MSXML2.DOMDocument.6.0") ' Disable validation when importing the XML doc.validateOnParse = False ' Enable the ability to import XML that contains <!DOCTYPE> doc.setProperty "ProhibitDTD", false ' Perform the import doc.loadXML "<!DOCTYPE html><html />" ' Display the imported XML WScript.Echo objXML.xml 

Finally, here is a copy of the code written in C ++.

 #include <stdio.h> #include <tchar.h> #include <msxml6.h> #import <msxml6.dll> #pragma comment(lib, "msxml6.lib") int _tmain(int argc, _TCHAR* argv[]) { HRESULT hr; // IXMLDOMDocument2 is needed for setProperty MSXML2::IXMLDOMDocument2 *doc; // Initialize COM hr = CoInitialize(NULL); if (SUCCEEDED(hr)) { // Create the object hr = CoCreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument2, (void**)&doc); if (SUCCEEDED(hr)) { // Disable validation when importing the XML doc->validateOnParse = VARIANT_FALSE; // Enable the ability to import XML that contains <!DOCTYPE> doc->setProperty(_bstr_t(L"ProhibitDTD"), VARIANT_FALSE); // Perform the import doc->loadXML(_bstr_t(L"<!DOCTYPE html><html />")); // Display the imported XML MessageBox(NULL, doc->xml, NULL, 0); } // Cleanup COM CoUninitialize(); } return 0; } 
+1
source

Source: https://habr.com/ru/post/1411183/


All Articles