C++中DOM写XML (转载)

本文详细介绍使用C++和MSXML4.0的DOM接口创建XML文档的过程。从初始化DOM对象到创建各种XML元素,包括处理指令、注释、文本节点等,并最终将构建好的XML文档保存至磁盘。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

用MSXML 4.0:
1)用一般的指针
2)用智能指针,比较简单

下面的例子用智能指针:
 步骤:
        

Programmatically, the dynamDOMsmart application performs the following steps:

  1. Creates an XML DOM instance (pXMLDom).
  2. Calls the createProcessInstruction method on pXMLDom. This creates a processing instruction node (pi) targeted for XML 1.0.
  3. Calls the appendChild method on pXMLDom. This adds the processing instruction node (pi) to pXMLDom.
  4. Calls the createComment method on the DOM object (pXMLDom) to create a comment node (pc) and then append it pXMLDom.
  5. Creates a <root> element as the document element, with a created attribute whose value is set to a string value of "using DOM". Adds this element (<root>) to the DOM object (pXMLDom).
  6. Creates a <node1> element with some character data as its content. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
  7. Creates a <node2> element that contains a CDATA section (pcd) with markup text. Appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
  8. Creates a <node3> element that contains a DOM document fragment (pdf). This fragment contains three other empty child elements: <subNode1>, <subNode2>, and <subNode3>. The code then appends this element (pe) to the document element (documentElement) of the DOM object (pXMLDom).
  9. Saves this dynamically created DOM object to the project's main directory, and prints the XML data in the application console.
源代码:
#include  < stdio.h >
#import 
< msxml4.dll >
using   namespace  MSXML2;
int  main( int  argc,  char *  argv[])
ExpandedBlockStart.gifContractedBlock.gif
{
IXMLDOMDocument3Ptr pXMLDom;
HRESULT hr;
CoInitialize(NULL);
hr 
= pXMLDom.CreateInstance(__uuidof(DOMDocument40));
if (FAILED(hr))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
printf(
"Failed to CreateInstance on an XML DOM");
return NULL;
}

pXMLDom
->preserveWhiteSpace = VARIANT_TRUE;
// Create a processing instruction targeted for xml.
IXMLDOMProcessingInstructionPtr pi;
pi 
= pXMLDom->createProcessingInstruction("xml""version='1.0'");
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (pi != NULL) {
pXMLDom
->appendChild(pi);
pi.Release();
}

// Create a processing instruction targeted for xml-stylesheet.
pi = pXMLDom->createProcessingInstruction("xml-stylesheet",
"type='text/xml' href='dom.xsl'");
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (pi != NULL) {
pXMLDom
->appendChild(pi);
pi.Release();
}

// Create a comment for the document.
IXMLDOMCommentPtr pc;
pc 
= pXMLDom->createComment("sample xml file created using XML DOM object.");
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (pc != NULL) {
pXMLDom
->appendChild(pc);
pc.Release();
}

// Create the root element (i.e., the documentElement).
IXMLDOMElementPtr pe;
pe 
= pXMLDom->createElement("root");
// Create a "created" attribute for the root element and
// assign the "using dom" character data as the attribute value.
IXMLDOMAttributePtr pa;
pa 
= pXMLDom->createAttribute("created");
if (pa != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
pa
->value = "using dom";
pe
->setAttributeNode(pa);
pa.Release();
}

// Add the root element to the DOM instance.
pXMLDom->appendChild(pe);
pe.Release();
// Next, we will create and add more nodes to the root element
// we've just created.
// Create an element to hold text content.
pe = pXMLDom->createElement("node1");
if (pe != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
// Add newline + tab for indentation.
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
pe
->text = "some character data";
pXMLDom
->documentElement->appendChild(pe);
pe.Release();
}

// Create an element to hold a CDATA section.
pe=pXMLDom->createElement("node2");
if (pe != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
// Add newline + tab for indentation.
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
IXMLDOMCDATASectionPtr pcd;
pcd 
= pXMLDom->createCDATASection("<some mark-up text>");
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (pcd != NULL) {
pe
->appendChild(pcd);
pcd.Release();
}

pXMLDom
->documentElement->appendChild(pe);
pe.Release();
}

// Create an element to hold three empty subelements.
pe=pXMLDom->createElement("node3");
if (pe != NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
// Add newline +tab for indentation.
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
IXMLDOMDocumentFragmentPtr pdf;
pdf 
= pXMLDom->createDocumentFragment();
pdf
->appendChild(pXMLDom->createTextNode("\n\t\t"));
pdf
->appendChild(pXMLDom->createElement("subNode1"));
pdf
->appendChild(pXMLDom->createTextNode("\n\t\t"));
pdf
->appendChild(pXMLDom->createElement("subNode2"));
pdf
->appendChild(pXMLDom->createTextNode("\n\t\t"));
pdf
->appendChild(pXMLDom->createElement("subNode3"));
pdf
->appendChild(pXMLDom->createTextNode("\n\t"));
pe
->appendChild(pdf);
pdf.Release();
pXMLDom
->documentElement->appendChild(pe);
pe.Release();
pXMLDom
->documentElement->appendChild(pXMLDom->createTextNode("\n"));
}

printf(
"Dynamically created DOM:\n%s\n",
(LPCSTR)pXMLDom
->xml);
hr 
= pXMLDom->save("dynaDom.xml");
if (FAILED(hr))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
printf(
"Failed to save DOM to dynaDom.xml\n");
}

else
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
printf(
"DOM saved to dynamDom.xml\n");
}

if (pXMLDom) pXMLDom.Release();
CoUninitialize();
return 0;
}


结果:
Dynamically created DOM:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xml' href='dom.xsl'?>
<!--sample xml file created using XML DOM object.-->
<root created="using dom">
<node1>some character data</node1>
<node2><![CDATA[<some mark-up text>]]></node2>
<node3>
<subNode1/>
<subNode2/>
<subNode3/>
</node3>
</root>
DOM saved to dynamDom.xml
-----------------------------
在此过程中,经常会有一些错误:保存文件的路径,有的时候我写绝对路径,但是结果它却还是保存到相对路径,(为什么那?)还有里面的字符格式的转化,比较复杂,哈哈!欢迎大家来讨论:

哪位高手知道,关于保存路径的具体的东西啊,反正我发现相对路径有的时候并不总是相对你的原程序,当你打开文件处理在保存时,相对路径是相对你程序打开的文件的路径!



还有其他的吗,该轮到你们拉:


posted on 2005-12-29 09:48 梦在天涯 阅读(1531) 评论(8)  编辑 收藏 引用 所属分类: CPlusPlusUML/XML

评论

 re: C++中DOM写XML 2005-12-29 13:44

为什么createelement(item)时,item不能是由数字转换为的字符串!而且是有时可以有时不可,都发现好多次拉,难道时bug,还是英文版的缘故啊?       

 re: C++中DOM写XML 2005-12-29 14:53

我来回答你

1.路径的问题
相对路径都是相对于当前的path,可能是可执行程序所在路径,跟源程序的路径无关,这对于任何win32的程序都一样

2.CreateElement为什么有时不能是数字?
因为xml的节点有些不能是数字
比如
<root>
<2>test</2>
</root>
不是有效的xml

<root>
<test>2</test>
</root>
就是有效的
       

 re: C++中DOM写XML 2005-12-29 16:09

恩,太感谢拉!牛!

为什么说有些节点不能是数字那?是所有的吗?好像有的行啊!^_^!


哦,想起来拉,节点的命名首字母不能是()。。。。。。。^_^!~

但是element里的text,我们不用管它是中文还是英文,也不用管是身编码吗?只要我们在xml头指定encoding=“”就可以了吗?为什么有的时候加了encoding =“gb2312”,显示仍然是乱麻那~???


谢谢!       

 re: C++中DOM写XML 2006-01-20 13:50

我的编译通不过啊!!!
把IXMLDOMDocument3Ptr 改为IXMLDOMDocumentPtr能通过???
上面的例子就是SDK里面的吧!       

 re: C++中DOM写XML 2006-01-20 15:22

请助!
上面的代码编译不过,提示说IXMLDOMDocument3Ptr没有定义!

在system32中没有msxml4a.dll ,而msxml4.dll 和 msxml4r.dll有.
请问:是因为缺那个DLL造成编译通不过的吗?如果是,这么解决啊!

恳求大侠帮助!!!!!!       

 re: C++中DOM写XML 2006-01-23 17:01

是啊,要看你装的MSXML的版本啊,有的要用3,有的不用啊!
好像是这样啊!       

 re: C++中DOM写XML 2006-02-08 20:50

原程序对有些机器不能通过的原因:


应该把IXMLDOMDocument3Ptr中的3改为2!

还有虽然用了namespace MSXML2,但是还必须在每个用到的定义如IXMLDOMElementPtr 前加上MSXML2::,这样便可以通过!



有知道为什么的吗?


       

 re: C++中DOM写XML 2007-12-27 20:35

怎么用C++解析xml文件呢?需要对xml文件提取每个行字符 请求帮助。谢谢
QQ:313054332  

转载于:https://www.cnblogs.com/jcss2008/archive/2009/01/06/1370570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值