大家好,欢迎来到IT知识分享网。
最近使用C++ DOM增删XML上的一个节点,以前没有写过,写的过程中也碰到一些问题,写完后整理了一下。运行在windows上,我用vs2015编译的。
参考了https://www.cnblogs.com/areliang/archive/2011/11/23/2260566.html
示例中,添加函数会创建如下的xml。
<?xml version="1.0" encoding="utf-8"?>
<Animals>
<dog>hei bei</dog>
<pig>pei qi</pig>
<Cat>hua mao</Cat>
<Cat name="jiafei" color="yellow">jia fei mao</Cat>
<Cat name="jingzhang" color="black">hei mao jing zhang</Cat>
</Animals>
#include <iostream>
#include <atlbase.h>
#include <msxml6.h>
#include <Windows.h>
#pragma comment(lib, "msxml6.lib")
using namespace std;
void CreateXmlAndAddNode()
{
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(CLSID_DOMDocument60);
if (SUCCEEDED(hr))
{
CComPtr<IXMLDOMProcessingInstruction> spDOMPI;
hr = spXmldoc->createProcessingInstruction(L"xml", L"version='1.0' encoding='utf-8'", &spDOMPI); // 创建声明
if (hr == S_OK)
{
CComPtr<IXMLDOMNode> outNode;
spXmldoc->appendChild(spDOMPI, &outNode); //添加首行节点
outNode.Detach();
CComPtr<IXMLDOMElement> pDOMRoot;
hr = spXmldoc->createElement(L"Animals", &pDOMRoot);
spXmldoc->appendChild(pDOMRoot, &outNode); //添加根节点
outNode.Detach();
if (hr == S_OK)
{
CComPtr<IXMLDOMText> textNode;
spXmldoc->createTextNode(L"\n\t", &textNode);
CComPtr<IXMLDOMElement> pDOMElement1;
pDOMRoot->appendChild(textNode, NULL);
textNode.Detach();
spXmldoc->createElement(L"dog", &pDOMElement1);
pDOMElement1->put_text(L"hei bei");
pDOMRoot->appendChild(pDOMElement1, NULL);
spXmldoc->createTextNode(L"\n\t", &textNode);
pDOMRoot->appendChild(textNode, NULL);
textNode.Detach();
CComPtr<IXMLDOMElement> pDOMElement2;
spXmldoc->createElement(L"pig", &pDOMElement2);
pDOMElement2->put_text(L"pei qi");
pDOMRoot->appendChild(pDOMElement2, NULL);
spXmldoc->createTextNode(L"\n\t", &textNode);
pDOMRoot->appendChild(textNode, NULL);
textNode.Detach();
CComPtr<IXMLDOMElement> pDOMElement3;
spXmldoc->createElement(L"Cat", &pDOMElement3);
pDOMElement3->put_text(L"hua mao");
pDOMRoot->appendChild(pDOMElement3, NULL);
spXmldoc->createTextNode(L"\n\t", &textNode);
pDOMRoot->appendChild(textNode, NULL);
textNode.Detach();
CComPtr<IXMLDOMElement> pDOMElement4;
spXmldoc->createElement(L"Cat", &pDOMElement4);
pDOMElement4->put_text(L"jia fei mao");
pDOMElement4->setAttribute(L"name", CComVariant("jiafei")); //添加属性
pDOMElement4->setAttribute(L"color", CComVariant("yellow"));
pDOMRoot->appendChild(pDOMElement4, NULL);
spXmldoc->createTextNode(L"\n\t", &textNode);
pDOMRoot->appendChild(textNode, NULL);
textNode.Detach();
CComPtr<IXMLDOMElement> pDOMElement5;
spXmldoc->createElement(L"Cat", &pDOMElement5);
pDOMElement5->put_text(L"hei mao jing zhang");
pDOMElement5->setAttribute(L"name", CComVariant("jingzhang"));
pDOMElement5->setAttribute(L"color", CComVariant("black"));
pDOMRoot->appendChild(pDOMElement5, NULL);
spXmldoc->createTextNode(L"\n", &textNode);
pDOMRoot->appendChild(textNode, NULL);
textNode.Detach();
}
}
spXmldoc->save(CComVariant("cppDOM.xml"));
spXmldoc.Detach();
}
CoUninitialize();
}
我的电脑里没有msxml4,我在system32下找到了msxml6.dll。试了一下也好用。需要引入这个库并包括头文件msxml6.h。这里要注意的是增加了一些空的textnode节点来换行,不加的话生成的xml内容只有一行。
删除函数://删除<Cat name=”jiafei” color=”yellow”>jia fei mao</Cat>
这里第一次接触xpath,用它来定位节点很方便。
void DeleteNode()
{
CoInitialize(NULL);
CComPtr<IXMLDOMDocument> spXmldoc;
HRESULT hr = spXmldoc.CoCreateInstance(CLSID_DOMDocument60);
if (SUCCEEDED(hr))
{
VARIANT_BOOL isSuccessful;
spXmldoc->load(CComVariant("cppDOM.xml"), &isSuccessful);
if (VARIANT_TRUE == isSuccessful)
{
CComPtr<IXMLDOMElement> spRoot = NULL;
hr = spXmldoc->get_documentElement(&spRoot);
if (hr == S_OK)
{
CComPtr<IXMLDOMNode> spNode;
//通过xpath寻找节点
hr = spRoot->selectSingleNode(L"/Animals/Cat[contains(@name, 'jiafei') and contains(@color, 'yellow')]", &spNode);
if (hr == S_OK)
{
spRoot->removeChild(spNode, NULL);//删除节点
}
}
}
}
spXmldoc->save(CComVariant("cppDOM.xml"));
spXmldoc.Detach();
CoUninitialize();
}
删除后的xml是。
<?xml version="1.0" encoding="utf-8"?>
<Animals>
<dog>hei bei</dog>
<pig>pei qi</pig>
<Cat>hua mao</Cat>
<Cat name="jingzhang" color="black">hei mao jing zhang</Cat>
</Animals>
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23977.html