如何用MFC实现读取和修改XML文件中的数据
2个回答
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);
}
把上面的代码放到两个按钮下就可以了
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);
}
把上面的代码放到两个按钮下就可以了
展开全部
写
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);
}
把上面的代码放到两个按钮下就可以了
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);
}
把上面的代码放到两个按钮下就可以了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询