热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C++中DOM写XML(转载)

用MSXML4.0:1)用一般的指针2)用智能指针,比较简单下面的例子用智能指针:步骤:Programmatically,t

 

用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 element as the document element, with a created attribute whose value is set to a string value of "using DOM". Adds this element () to the DOM object (pXMLDom).
  6. Creates a 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 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 element that contains a DOM document fragment (pdf). This fragment contains three other empty child elements: , , and . 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 
&#61; pXMLDom.CreateInstance(__uuidof(DOMDocument40));
if (FAILED(hr))
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
printf(
"Failed to CreateInstance on an XML DOM");
return NULL;
}

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

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

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

// Create the root element (i.e., the documentElement).
IXMLDOMElementPtr pe;
pe 
&#61; 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 
&#61; pXMLDom->createAttribute("created");
if (pa !&#61; NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
pa
->value &#61; "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&#39;ve just created.
// Create an element to hold text content.
pe &#61; pXMLDom->createElement("node1");
if (pe !&#61; NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
// Add newline &#43; tab for indentation.
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
pe
->text &#61; "some character data";
pXMLDom
->documentElement->appendChild(pe);
pe.Release();
}

// Create an element to hold a CDATA section.
pe&#61;pXMLDom->createElement("node2");
if (pe !&#61; NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
// Add newline &#43; tab for indentation.
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
IXMLDOMCDATASectionPtr pcd;
pcd 
&#61; pXMLDom->createCDATASection("");
ExpandedSubBlockStart.gifContractedSubBlock.gif
if (pcd !&#61; NULL) {
pe
->appendChild(pcd);
pcd.Release();
}

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

// Create an element to hold three empty subelements.
pe&#61;pXMLDom->createElement("node3");
if (pe !&#61; NULL)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
// Add newline &#43;tab for indentation.
pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));
IXMLDOMDocumentFragmentPtr pdf;
pdf 
&#61; 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 
&#61; 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;
}


结果&#xff1a;

Dynamically created DOM:




some character data
]]>






DOM saved to dynamDom.xml &#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;&#xff0d;
在此过程中&#xff0c;经常会有一些错误&#xff1a;保存文件的路径&#xff0c;有的时候我写绝对路径&#xff0c;但是结果它却还是保存到相对路径&#xff0c;&#xff08;为什么那&#xff1f;&#xff09;还有里面的字符格式的转化&#xff0c;比较复杂&#xff0c;哈哈&#xff01;欢迎大家来讨论&#xff1a;

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



还有其他的吗&#xff0c;该轮到你们拉&#xff1a;


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

评论

 re: C&#43;&#43;中DOM写XML 2005-12-29 13:44

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

 re: C&#43;&#43;中DOM写XML 2005-12-29 14:53

我来回答你

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

2.CreateElement为什么有时不能是数字&#xff1f;
因为xml的节点有些不能是数字
比如

<2>test

不是有效的xml


2

就是有效的
       

 re: C&#43;&#43;中DOM写XML 2005-12-29 16:09

恩&#xff0c;太感谢拉&#xff01;牛&#xff01;

为什么说有些节点不能是数字那&#xff1f;是所有的吗&#xff1f;好像有的行啊&#xff01;^_^&#xff01;


哦&#xff0c;想起来拉&#xff0c;节点的命名首字母不能是&#xff08;&#xff09;。。。。。。。^_^&#xff01;&#xff5e;

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


谢谢&#xff01;       

 re: C&#43;&#43;中DOM写XML 2006-01-20 13:50

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

 re: C&#43;&#43;中DOM写XML 2006-01-20 15:22

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

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

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

 re: C&#43;&#43;中DOM写XML 2006-01-23 17:01

是啊&#xff0c;要看你装的MSXML的版本啊&#xff0c;有的要用3&#xff0c;有的不用啊&#xff01;
好像是这样啊&#xff01;       

 re: C&#43;&#43;中DOM写XML 2006-02-08 20:50

原程序对有些机器不能通过的原因&#xff1a;


应该把IXMLDOMDocument3Ptr中的3改为2&#xff01;

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



有知道为什么的吗&#xff1f;


       

 re: C&#43;&#43;中DOM写XML 2007-12-27 20:35

怎么用C&#43;&#43;解析xml文件呢&#xff1f;需要对xml文件提取每个行字符 请求帮助。谢谢
QQ&#xff1a;313054332  


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



推荐阅读
author-avatar
Happy_Kelly尊荣
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有