保存不带结束标记的 IXMLDOMDocument

发布于 2024-07-16 21:24:06 字数 321 浏览 6 评论 0原文

通过 C++ 使用 MSXML,当我调用 IXMLDOMDocument::save 时,空 XML 元素将获得一个结束标记,如下所示:

<root>
    <child name="first">
    </child>
</root>

但我希望将其保存为这样:

<root>
    <child name="first" />
</root>

我需要做什么才能完成此操作?

Using MSXML via C++, when I call IXMLDOMDocument::save, empty XML elements will get an end tag, like this:

<root>
    <child name="first">
    </child>
</root>

But I want it saved as this:

<root>
    <child name="first" />
</root>

What do I need to do to accomplish that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

于我来说 2024-07-23 21:24:06

如果没有子节点,DOM 本身就会这样做,但我不知道为什么在你的情况下它会这样做。
如果你想在 DOM 指针上显式调用 Pretty Print。

#include <msxml2.h>

bool FormatDOMDocument (IXMLDOMDocument *pDoc, IStream *pStream)
{

     // Create the writer
     CComPtr <IMXWriter> pMXWriter;
    if (FAILED (pMXWriter.CoCreateInstance(__uuidof (MXXMLWriter), NULL, CLSCTX_ALL)))
    {
        return false;
    }
    CComPtr <ISAXContentHandler> pISAXContentHandler;
    if (FAILED (pMXWriter.QueryInterface(&pISAXContentHandler)))
    {
        return false;
    }
    CComPtr <ISAXErrorHandler> pISAXErrorHandler;
    if (FAILED (pMXWriter.QueryInterface (&pISAXErrorHandler)))
    {
        return false;
    }
    CComPtr <ISAXDTDHandler> pISAXDTDHandler;
    if (FAILED (pMXWriter.QueryInterface (&pISAXDTDHandler)))
    {
        return false;
    }

   if (FAILED (pMXWriter ->put_omitXMLDeclaration (VARIANT_FALSE)) ||
       FAILED (pMXWriter ->put_standalone (VARIANT_TRUE)) ||
       FAILED (pMXWriter ->put_indent (VARIANT_TRUE)) ||
       FAILED (pMXWriter ->put_encoding (L"UTF-8")))
   {
       return false;
   }

   CComPtr <ISAXXMLReader> pSAXReader;
   if (FAILED (pSAXReader.CoCreateInstance (__uuidof (SAXXMLReader), NULL, CLSCTX_ALL)))
   {
      return false;
   }

   if (FAILED (pSAXReader ->putContentHandler (pISAXContentHandler)) ||
    FAILED (pSAXReader ->putDTDHandler (pISAXDTDHandler)) ||
    FAILED (pSAXReader ->putErrorHandler (pISAXErrorHandler)) ||
    FAILED (pSAXReader ->putProperty (
    L"http://xml.org/sax/properties/lexical-handler", CComVariant (pMXWriter))) ||
    FAILED (pSAXReader ->putProperty (
    L"http://xml.org/sax/properties/declaration-handler", CComVariant (pMXWriter))))
   {
      return false;
   }

   // Perform the write

   return SUCCEEDED (pMXWriter ->put_output (CComVariant (pStream))) &&
   SUCCEEDED (pSAXReader ->parse (CComVariant (pDoc)));
}

DOM itself does that if there are no child nodes but I don't know why in your case it is doing like this.
If you want to do explicitly call Pretty Print on the DOM pointer.

#include <msxml2.h>

bool FormatDOMDocument (IXMLDOMDocument *pDoc, IStream *pStream)
{

     // Create the writer
     CComPtr <IMXWriter> pMXWriter;
    if (FAILED (pMXWriter.CoCreateInstance(__uuidof (MXXMLWriter), NULL, CLSCTX_ALL)))
    {
        return false;
    }
    CComPtr <ISAXContentHandler> pISAXContentHandler;
    if (FAILED (pMXWriter.QueryInterface(&pISAXContentHandler)))
    {
        return false;
    }
    CComPtr <ISAXErrorHandler> pISAXErrorHandler;
    if (FAILED (pMXWriter.QueryInterface (&pISAXErrorHandler)))
    {
        return false;
    }
    CComPtr <ISAXDTDHandler> pISAXDTDHandler;
    if (FAILED (pMXWriter.QueryInterface (&pISAXDTDHandler)))
    {
        return false;
    }

   if (FAILED (pMXWriter ->put_omitXMLDeclaration (VARIANT_FALSE)) ||
       FAILED (pMXWriter ->put_standalone (VARIANT_TRUE)) ||
       FAILED (pMXWriter ->put_indent (VARIANT_TRUE)) ||
       FAILED (pMXWriter ->put_encoding (L"UTF-8")))
   {
       return false;
   }

   CComPtr <ISAXXMLReader> pSAXReader;
   if (FAILED (pSAXReader.CoCreateInstance (__uuidof (SAXXMLReader), NULL, CLSCTX_ALL)))
   {
      return false;
   }

   if (FAILED (pSAXReader ->putContentHandler (pISAXContentHandler)) ||
    FAILED (pSAXReader ->putDTDHandler (pISAXDTDHandler)) ||
    FAILED (pSAXReader ->putErrorHandler (pISAXErrorHandler)) ||
    FAILED (pSAXReader ->putProperty (
    L"http://xml.org/sax/properties/lexical-handler", CComVariant (pMXWriter))) ||
    FAILED (pSAXReader ->putProperty (
    L"http://xml.org/sax/properties/declaration-handler", CComVariant (pMXWriter))))
   {
      return false;
   }

   // Perform the write

   return SUCCEEDED (pMXWriter ->put_output (CComVariant (pStream))) &&
   SUCCEEDED (pSAXReader ->parse (CComVariant (pDoc)));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文