如何用MFC实现读取和修改XML文件中的数据

 我来答
匿名用户
2017-08-14
展开全部

MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot;
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));

if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库");
return;
}

pDoc->raw_createElement((_bstr_t)(char*)"china",&xmlRoot);
pDoc->raw_appendChild(xmlRoot,NULL);

MSXML2::IXMLDOMElementPtr childNode;
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode);
childNode->put_text((_bstr_t)(char*)"WuHan");
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("8,000,000"));
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("10000"));
xmlRoot->appendChild(childNode);

pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode);
childNode->put_text((_bstr_t)(char*)"ShangHai");
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("12,000,000"));
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("12000"));
xmlRoot->appendChild(childNode);

pDoc->save("e:\\he.xml");


MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库");
return;
}
pDoc->load("e:\\he.xml");
MSXML2::IXMLDOMElementPtr childNode;
childNode=(MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"));
MSXML2::DOMNodeType nodeType;
childNode->get_nodeType(&nodeType);
BSTR var;
childNode->get_nodeName(&var);
CString name=(char*)(_bstr_t)var;
VARIANT varVal;
childNode->get_nodeTypedValue(&varVal);
CString strValue=(char*)(_bstr_t)varVal;

MSXML2::IXMLDOMNamedNodeMapPtr pAttrs=NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
childNode->get_attributes(&pAttrs);
long nCount;
pAttrs->get_length(&nCount);
for(int i=0;i<nCount;i++)
{
pAttrs->get_item(i,&pAttrItem);
CString strAttrName=(char*)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue=(char*)(_bstr_t)pAttrItem->nodeTypedValue;
AfxMessageBox(strAttrName+strAttrValue);
}

把上面的代码放到两个按钮下就可以了
就烦条0o
2017-08-14 · 知道合伙人软件行家
就烦条0o
知道合伙人软件行家
采纳数:33315 获赞数:46492
从事多年系统运维,喜欢编写各种小程序和脚本。

向TA提问 私信TA
展开全部

MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot;
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库");
return;
}
pDoc->raw_createElement((_bstr_t)(char*)"china",&xmlRoot);
pDoc->raw_appendChild(xmlRoot,NULL);
MSXML2::IXMLDOMElementPtr childNode;
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode);
childNode->put_text((_bstr_t)(char*)"WuHan");
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("8,000,000"));
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("10000"));
xmlRoot->appendChild(childNode);
pDoc->raw_createElement((_bstr_t)(char*)"City",&childNode);
childNode->put_text((_bstr_t)(char*)"ShangHai");
childNode->setAttribute((_bstr_t)(char*)"population",CComVariant("12,000,000"));
childNode->setAttribute((_bstr_t)(char*)"area",CComVariant("12000"));
xmlRoot->appendChild(childNode);
pDoc->save("e:\\he.xml");

MSXML2::IXMLDOMDocumentPtr pDoc;
HRESULT hr=pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
if (FAILED(hr))
{
AfxMessageBox("无法创建对象,是否安装了MS XML运行库");
return;
}
pDoc->load("e:\\he.xml");
MSXML2::IXMLDOMElementPtr childNode;
childNode=(MSXML2::IXMLDOMElementPtr)(pDoc->selectSingleNode("//City"));
MSXML2::DOMNodeType nodeType;
childNode->get_nodeType(&nodeType);
BSTR var;
childNode->get_nodeName(&var);
CString name=(char*)(_bstr_t)var;
VARIANT varVal;
childNode->get_nodeTypedValue(&varVal);
CString strValue=(char*)(_bstr_t)varVal;
MSXML2::IXMLDOMNamedNodeMapPtr pAttrs=NULL;
MSXML2::IXMLDOMNodePtr pAttrItem;
childNode->get_attributes(&pAttrs);
long nCount;
pAttrs->get_length(&nCount);
for(int i=0;i<nCount;i++)
{
pAttrs->get_item(i,&pAttrItem);
CString strAttrName=(char*)(_bstr_t)pAttrItem->nodeName;
CString strAttrValue=(char*)(_bstr_t)pAttrItem->nodeTypedValue;
AfxMessageBox(strAttrName+strAttrValue);
}
把上面的代码放到两个按钮下就可以了
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式