如何让用VC++生成的XML文档,能够按照缩进方式保存?

时间:2021-05-11 11:54:09
#include "comdef.h"
#import "C:\WINDOWS\system32\msxml.dll"
void CMyTestXMl3Dlg::OnBnClickedButton1()
{
    MSXML::IXMLDOMDocumentPtr pDoc; 
    pDoc.CreateInstance(__uuidof(MSXML::DOMDocument));
    MSXML::IXMLDOMElementPtr pDocElement=pDoc->createElement("China");
    pDoc->appendChild(pDocElement);
    MSXML::IXMLDOMElementPtr pNewChildElement; 
    pNewChildElement=pDoc->createElement("Beijing"); 
    pNewChildElement->Puttext("-11"); 
    pDocElement->appendChild(pNewChildElement); 
    pNewChildElement=pDoc->createElement("Shanghai"); 
    pNewChildElement->Puttext("9"); 
    pNewChildElement->setAttribute("Weather",_variant_t("Cloudy")); pDocElement->appendChild(pNewChildElement); 
    pDoc->save(_variant_t(L"c:\\测试.xml"));
}

12 个解决方案

#1


Hi,没写清楚问题,在问题后面增加一句话:
所保存的XML文件不会换行和缩格,应该如何实现该功能?

#2


告诉你一个简单的办法,只要打开XML文档再保存XML文档就OK了,也就是在你的代码后面加上

pDoc->load(_variant_t(L"c:\\测试.xml"));
pDoc->save(_variant_t(L"c:\\测试.xml"));

就OK了。

#3


可以自己解析
楼上的方法不工作

#4


用XMLSpy,很容易就对奇了

#5



用SAX方式  transform一下

#6


用ie打开就看到了,我想。

#7


- <China>
  <Beijing>-11</Beijing> 
  <Shanghai Weather="Cloudy">9</Shanghai> 
  </China>

#8


zscpsoft(zscpsoft) ( ) 信誉:98 
--------------
你的方法不行

#9


看这里
http://spaces.msn.com/x5min/Blog/cns!69B9CFBDDC44F76A!215.entry

#10


我是自己填空格对

#11


还是自己构造吧,以前做过,代码不在了

#12


'Create the DOMDocument object.
    Dim xmlDoc As New MSXML2.DOMDocument40
    'Create an MXXMLWriter object.
    Dim wrt As New MXXMLWriter40
    'Reference the ContentHandler interface.
    Dim cnth As IVBSAXContentHandler

    'Set the writer as the content handler.
    Set cnth = wrt
    'Set the writer output to the xmlDoc object.
    wrt.output = xmlDoc

wrt.indent=true 

参考一下吧。也许有用……

#1


Hi,没写清楚问题,在问题后面增加一句话:
所保存的XML文件不会换行和缩格,应该如何实现该功能?

#2


告诉你一个简单的办法,只要打开XML文档再保存XML文档就OK了,也就是在你的代码后面加上

pDoc->load(_variant_t(L"c:\\测试.xml"));
pDoc->save(_variant_t(L"c:\\测试.xml"));

就OK了。

#3


可以自己解析
楼上的方法不工作

#4


用XMLSpy,很容易就对奇了

#5



用SAX方式  transform一下

#6


用ie打开就看到了,我想。

#7


- <China>
  <Beijing>-11</Beijing> 
  <Shanghai Weather="Cloudy">9</Shanghai> 
  </China>

#8


zscpsoft(zscpsoft) ( ) 信誉:98 
--------------
你的方法不行

#9


看这里
http://spaces.msn.com/x5min/Blog/cns!69B9CFBDDC44F76A!215.entry

#10


我是自己填空格对

#11


还是自己构造吧,以前做过,代码不在了

#12


'Create the DOMDocument object.
    Dim xmlDoc As New MSXML2.DOMDocument40
    'Create an MXXMLWriter object.
    Dim wrt As New MXXMLWriter40
    'Reference the ContentHandler interface.
    Dim cnth As IVBSAXContentHandler

    'Set the writer as the content handler.
    Set cnth = wrt
    'Set the writer output to the xmlDoc object.
    wrt.output = xmlDoc

wrt.indent=true 

参考一下吧。也许有用……