如何设置XMLDOMDocument的doctype?

时间:2012-05-07 21:09:35

标签: html5 doctype msxml

如何设置doctype对象的DOMDocument60

例如我尝试:

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

除了IXMLDOMDocumentType.name 只读

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

IXMLDOMDocument60.doctype 只读

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

那么如何设置XML文档的doctype


加分问题:如何创建具有指定DOMDocument60的{​​{1}}对象?


注意:您没有看到XSLT,因为没有。我正在用MSXML构建一个HTML DOM树。

1 个答案:

答案 0 :(得分:1)

对于perfomance reasonssecurity reasons,Microsoft通常不允许<!DOCTYPE>(又称Document Type Definition)。因此,您必须使用loadXML方法设置<!DOCTYPE>。因此,在创建或导入文档后无法设置。

最重要的是,由于MSXML6中的默认安全设置,您通常无法导入具有<!DOCTYPE>的XML。因此,您必须禁用对象上的ProhibitDTD设置。

修改:您应该知道HTML5 is not XML。此外,<!DOCTYPE>considered optional for XHTML5

首先,让我们从所需的输出开始。

<!DOCTYPE html>
<html />

根据语法,我假设您正在使用C#并添加了对msxml6.dll的引用。以下代码将允许您创建这两个处理指令。

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);

这里也是用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

最后,这里是用C ++编写的代码的副本。

#include <comutil.h>
#pragma comment(lib, "comsuppw.lib")
#include <msxml6.h>
#pragma comment(lib, "msxml6.lib")

int main(int argc, char* argv[])
{

    HRESULT hr = S_OK;
    VARIANT_BOOL success = VARIANT_TRUE;
    // IXMLDOMDocument2 is needed for setProperty
    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
            hr = doc->put_validateOnParse(VARIANT_FALSE);
            // Enable the ability to import XML that contains <!DOCTYPE>
            hr = doc->setProperty(_bstr_t(L"ProhibitDTD"), _variant_t(VARIANT_FALSE));
            // Perform the import
            hr = doc->loadXML(_bstr_t(L"<!DOCTYPE html><html />"), &success);
            // Retrieve the XML
            _bstr_t output{};
            hr = doc->get_xml(output.GetAddress());
            // Display the imported XML
            MessageBoxW(NULL, output, NULL, 0);
        }
        // Cleanup COM
        CoUninitialize();
    }
    return 0;
}