如何设置 XMLDOMDocument 的 doctype?

问题描述 投票:0回答:2

如何设置

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
doctype
对象?


注意: 您看不到任何提及 XSLT 的内容,因为根本就没有。我正在 MSXML 中构建 HTML DOM 树。

html doctype msxml
2个回答
3
投票

出于性能原因安全原因,Microsoft 通常不允许

<!DOCTYPE>
(又名文档类型定义)。因此,您必须使用
loadXML
方法来设置
<!DOCTYPE>
。因此,在创建或导入文档后无法对其进行设置。

最重要的是,由于 MSXML6 中的默认安全设置,您通常无法导入具有

<!DOCTYPE>
的 XML。因此,您必须禁用对象上的
ProhibitDTD
设置。

编辑:您应该知道HTML5 不是XML。此外,

<!DOCTYPE>
对于 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;
}

0
投票

您可以在空文档中添加“处理指令”

doc := domDocument60::new(),
PI = doc:createProcessingInstruction('xml', 'version="1.0" encoding="utf-8"'),
_ = doc:appendChild(PI),

我希望这个想法应该很清楚,但你必须自己翻译成你最喜欢的语言。

© www.soinside.com 2019 - 2024. All rights reserved.