markdown test2
#home {
width: auto;
margin: 0 80px 0 0;
}
mkdir test.
#include <msxml6.h> // 含有 MSXML最新版
#include <atlbase.h>
#include "atlstr.h" // 含有CString, CStringW和CW2A
#include <iostream> // 包含wcout函数
#include <string> // 包含 c_str()函数, wcout
#include "comutil.h" // 包含_bstr_t
using namespace std;
const wchar_t *src = L""
L"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n"
L"<root desc=\"Great\">\r\n"
L" <text>Hey</text>\r\n"
L" <layouts>\r\n"
L" <lay index=\"15\" bold=\"true\"/>\r\n"
L" <layoff index=\"12\"/>\r\n"
L" <layin index=\"17\"/>\r\n"
L" </layouts>\r\n"
L"</root>\r\n";
int main()
{
CoInitialize(NULL); // Initialize COM
CComPtr<IXMLDOMDocument> iXMLDoc; // Or use CComPtr<IXMLDOMDocument2>, CComPtr<IXMLDOMDocument3>
try
{
HRESULT hr = iXMLDoc.CoCreateInstance(__uuidof(DOMDocument));
// iXMLDoc.CoCreateInstance(__uuidof(DOMDocument60));
// Load the file.
VARIANT_BOOL bSuccess = false;
// Load it from a url/filename...
hr = iXMLDoc->load(CComVariant(L"./test.xml"), &bSuccess);
// filePath = "./test.xml";
// hr = iXMLDoc->load(CComVariant(filePath.c_str()), &bSuccess);
// or from a BSTR...
// iXMLDoc->loadXML(CComBSTR(src), &bSuccess);
// Get a smart pointer (sp) to the root
CComPtr<IXMLDOMElement> pRootElement;
hr = iXMLDoc->get_documentElement(&pRootElement); // Root elements
// Get Attribute value of the note "root"
CComBSTR ssDesc("desc");
CComVariant deVal(VT_EMPTY);
hr = pRootElement->getAttribute(ssDesc, &deVal);
CComBSTR sstrRoot(L"root"); // sstrRoot("root");
CComPtr<IXMLDOMNode> rootNode;
hr = iXMLDoc->selectSingleNode(sstrRoot, &rootNode); // Search "root"
CComBSTR rootText;
hr = rootNode->get_text(&rootText);
if (SUCCEEDED(hr))
{
wstring bstrText(rootText);
wcout << "Text of root: " << bstrText << endl;
}
CComPtr<IXMLDOMNode> descAttribute;
hr = rootNode->selectSingleNode(CComBSTR("@desc"), &descAttribute); // Atrribute需要用@, 而各个节点不能使用@作为前缀来搜索
CComBSTR descVal;
hr = descAttribute->get_text(&descVal);
if (SUCCEEDED(hr))
{
wstring bstrText(descVal);
wcout << "Desc Attribute: " << bstrText << endl;
}
if (!FAILED(hr))
{
wstring strVal;
if (deVal.vt == VT_BSTR)
strVal = deVal.bstrVal;
wcout << "desc: " << strVal << endl;
}
CComPtr<IXMLDOMNodeList> pNodeList;
pRootElement->get_childNodes(&pNodeList); // Child node list
long nLen;
pNodeList->get_length(&nLen); // Child node list
for (long i = 0; i != nLen; ++i) // Traverse
{
CComPtr<IXMLDOMNode> pNode;
hr = pNodeList->get_item(i, &pNode);
CComBSTR ssName;
CComVariant val(VT_EMPTY);
hr = pNode->get_nodeName(&ssName);
if (SUCCEEDED(hr))
{
wstring bstrText(ssName);
wcout << "Name of node " << (i + 1) << ": " << bstrText << endl;
CString cstring(ssName);
// To display a CStringW correctly, use wcout and cast cstring to (LPCTSTR), an easier way to display wide character strings.
wcout << (LPCTSTR)cstring << endl;
// CW2A converts the string in ccombstr to a multi-byte string in printstr, used for display output.
CW2A printstr(ssName);
cout << printstr << endl;
}
}
// Add(Append) node
CComPtr<IXMLDOMDocument>& xmlDocData(iXMLDoc);
CComPtr<IXMLDOMElement> imageElement;
CComPtr<IXMLDOMNode> newImageNode;
string imageType = "jpeg";
char buffer[MAX_PATH];
GetCurrentDirectory(MAX_PATH, buffer); // Get Current Directory
string path(buffer); // Copy content of char*, generate a string
string imagePath = path + "\\com.jpg";
xmlDocData->createElement(CComBSTR(L"Image"), &imageElement);
imageElement->setAttribute(CComBSTR(L"Type"), CComVariant(CComBSTR(imageType.c_str()))); // 为当前节点添加属性
imageElement->setAttribute(CComBSTR(L"FileName"), CComVariant(CComBSTR(imagePath.c_str())));
rootNode->appendChild(imageElement, &newImageNode);
// Remove "text" node under "root" node
CComPtr<IXMLDOMNode> xmlOldNode;
CComPtr<IXMLDOMNode> textNode;
hr = rootNode->selectSingleNode(CComBSTR(L"text"), &textNode); // Search "text" node
hr = rootNode->removeChild(textNode, &xmlOldNode);
// Update XML
hr = iXMLDoc->save(CComVariant("updated.xml"));
}
catch (char* pStrErr) {
// Some error...
std::cout << pStrErr << std::endl << std::endl;
} // catch
catch (...) {
// Unknown error...
std::cout << "Unknown error..." << std::endl << std::endl;
}
// Release() - that gets done automatically, also can manually do for each opened node or elements.
// iXMLDoc.Release();
// Stop COM
CoUninitialize();
system("pause");
return 0;
}
运行结果:

运行完,得到的update.xml内容为:
https://raw.githubusercontent.com/yanglr/SimpleParser4MSXML-cpp/master/msxmlDemo/updated.xml
参考资料:
- IXMLDOMElement接口
- Using the MSXML Parser
- MFC C++ XML Parse - Using MSXML
- 如何:各种字符串类型之间转换 | Microsoft Docs
bottom
cc
markdown test2的更多相关文章
- markdown文本转换word,pdf
pandoc及下载和安装 pandoc是什么 pandoc是一个软件,是一个能把千奇百怪的文档格式互相转换的神器,是一把文档转换的瑞士军刀(swiss-army knife).不多说,放一张其官网(h ...
- Note | Markdown
目录 一.代码段 1.简单代码 2.大段代码 二.块注释 Blockquote 三.标题设置 四.字体 1.斜体 2.粗体 3.下划线 方案1:行内 HTML 方案2:html的span标签.设置行内 ...
- 使用vscode 编写Markdown文件
markdown简单语法参考下面简单事例: # 一级标题 1. 有序列表1 >1. 有序列表1 >>- *test1* >>- **test2** >>- * ...
- 马克飞象markdown用法
目录 markdown用法 ### 根据标题生成目录 `` 快捷键 ctrl+k 代码区域 ctrl+2 二级标题 ctrl+b/i 粗体/斜体 ctrl+l 插入链接 ctrl+g 插入图片 ctr ...
- 在VSCode使用Markdown绘制UML图
在VSCode使用Markdown绘制UML图 需要插件 Markdown All in One Markdown Preview Enhanced PlantUML markdownlint Mar ...
- React 可视化开发工具 Shadow Widget 非正经入门(之六:markdown)
本系列博文从 Shadow Widget 作者的视角,解释该框架的设计要点.本篇讲解 Markdown 在 Shadow Widget 中的应用. Markdown 在 Shadow Widget 中 ...
- NiceMark——我的Markdown编辑器
NiceMark--我的Markdown编辑器 闲来无事,写了一个Markdown编辑器.基于electron,完全采用Web前段技术(Html,css,JavaScript)实现.代码已托管在Git ...
- Markdown 图片助手-MarkdownPicPicker
title: Markdown 图片助手 v0.1 toc: true comments: true date: 2016-06-04 16:40:06 tags: [Python, Markdown ...
- 前端学Markdown
前面的话 我个人理解,Markdown就是一个富文本编辑器语言,类似于sass对于css的功能,Markdown也可以叫做HTML预处理器,只不过它是一门轻量级的标记语言,可以更简单的实现HTML ...
随机推荐
- 使用yield生成器,用Python实现用户对用户输入信息的监听和过滤
# -*- coding:utf-8 -*-'''''''''生成器是一次生成一个值的特殊类型函数.可以将其视为可恢复函数.调用该函数将返回一个可用于生成连续 x 值的生成[Generator],简单 ...
- 新人上手:如何做好一个App的推广?
App推广是现在所有公司都绕不开的槛,而一般App推广又分为线上线下两个方面,其中,App线上推广是互联网时代运营人员接触最多的一种推广方式.一款App应用推广的最终目的是为了吸引目标用户,为推销的产 ...
- eclipse 遇到的问题及解决思路
招黑的我和eclipse相冲,莫名其妙出现一堆问题.现在打算不定时更新把我遇到的问题更上来,解决方法也附上,不一定适用以后遇到的问题,可以是提供一种解决问题的思路. 1.eclipse配置问题(jar ...
- 什么是一致性Hash算法?
一.Redis集群的使用 我们在使用Redis的时候,为了保证Redis的高可用,提高Redis的读写性能,最简单的方式我们会做主从复制,组成Master-Master或者Master-Slave的形 ...
- jps命令详解
JPS 名称: jps - Java Virtual Machine Process Status Tool 命令用法: jps [options] [hostid] options:命令选项,用来对 ...
- LigerUI之Grid使用详解(一)——显示数据 --分页
http://www.cnblogs.com/jerehedu/p/4218560.html 首先给大家介绍最常用的数据展示组件Grid,使用步骤如下: 1.页面中正确引入样式文件及相应组件 < ...
- GET和POST两种基本请求方法(转自博主--在途中#)
GET和POST两种基本请求方法的区别 GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过req ...
- WPF线程中获取控件的值和给控件赋值
WPF中使用线程操作控件,按平常的操作方法操作的话会报异常:调用线程无法访问此对象,因为另一个线程拥有该对象.所以我们要使用Dispatcher类的BeginInvoke()与Invoke()方法.B ...
- StrictRedis
StrictRedis对象⽅法 通过init创建对象,指定参数host.port与指定的服务器和端⼝连接,host默认为localhost,port默认为6379,db默认为0 sr = Strict ...
- java.lang.IllegalAccessError: org.apache.commons.dbcp.DelegatingPreparedStatement.isClosed()Z
做spring和mybaits整合时出现的错误,让这个问题困扰了一早上,通过查资料终于把这个问题解决了 具体问题描述: java.lang.IllegalAccessError: org.apache ...