// XMLSettings.cpp : implementation file
//
#include "stdafx.h"
#include "BM.h"
#include "XMLFun.h"
#include <algorithm>
/////////////////////////////////////////////////////////////////////////////
// CXMLFun
CXMLFun::CXMLFun()
{
XmlDocPtr = NULL;
xml_file_name = "config.xml";
}
CXMLFun::~CXMLFun()
{
if(XmlDocPtr!=NULL)
XmlDocPtr.Detach();
}
CXMLFun::CXMLFun(CONST CHAR* filename)
{
::CoInitialize(NULL);
if(XmlDocPtr!=NULL)
XmlDocPtr.Detach();
XmlDocPtr = NULL;
xml_file_name=filename;
load(filename);
}
void CXMLFun::clear()
{
if(XmlDocPtr!=NULL)
XmlDocPtr.Detach();
DeleteFile(xml_file_name.c_str());
load(xml_file_name.c_str());
}
// get a LONG value
LONG CXMLFun::GetLong(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, LONG lDefaultValue)
{
/*
Since XML is text based and we have no schema, just convert to a string and
call the GetString method.
*/
LONG lRetVal = lDefaultValue;
CHAR chs[256];
sprintf(chs,"%d", lRetVal);
lRetVal = atol(GetString(cstrBaseKeyName, cstrValueName, chs).c_str() );
return lRetVal;
}
std::string CXMLFun::GetAttribute(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, CONST CHAR* cstrAttributeName, CONST CHAR* cstrDefaultAttributeValue)
{
std::string strAttributeValue;
std::string strDummy;
GetNodeValue(cstrBaseKeyName, cstrValueName, NULL, strDummy, cstrAttributeName,
cstrDefaultAttributeValue, strAttributeValue);
return strAttributeValue;
}
// get a string value
std::string CXMLFun::GetString(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, CONST CHAR* cstrDefaultValue)
{
std::string strValue;
std::string strDummy;
GetNodeValue(cstrBaseKeyName, cstrValueName, cstrDefaultValue, strValue, NULL, NULL, strDummy);
return strValue;
}
// set a LONG value
LONG CXMLFun::SetLong(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, LONG lValue)
{
LONG lRetVal = 0;
CHAR chsVal[256];
sprintf(chsVal,"%d", lValue);
lRetVal = SetString(cstrBaseKeyName, cstrValueName, chsVal);
return lRetVal;
}
// set a string value
LONG CXMLFun::SetString(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, CONST CHAR* cstrValue)
{
return SetNodeValue(cstrBaseKeyName, cstrValueName,cstrValue);
}
// set a string Attribute
LONG CXMLFun::SetAttribute(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, CONST CHAR* cstrAttributeName, CONST CHAR* cstrAttributeValue)
{
return SetNodeValue(cstrBaseKeyName, cstrValueName, NULL, cstrAttributeName, cstrAttributeValue);
}
LONG CXMLFun::GetNodeValue(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, CONST CHAR* cstrDefaultValue, std::string& strValue, CONST CHAR* cstrAttributeName, CONST CHAR* cstrDefaultAttributeValue,std::string& strAttributeValue)
{
INT iNumKeys = 0;
std::string cstrValue = cstrDefaultValue;
std::string* pCStrKeys = NULL;
std::string strBaseKeyName("//");
strBaseKeyName +=cstrBaseKeyName;
if( strBaseKeyName.at(strBaseKeyName.length() -1) !='/' )
strBaseKeyName += "/";
strBaseKeyName += cstrValueName;
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
foundNode=XmlDocPtr->selectSingleNode( _com_util::ConvertStringToBSTR(strBaseKeyName.c_str()) );
if (foundNode)
{
// get the text of the node (will be the value we requested)
BSTR bstr = NULL;
HRESULT hr = foundNode->get_text(&bstr);
strValue =_com_util::ConvertBSTRToString(bstr);
if(cstrAttributeName!=NULL)
{
MSXML2::IXMLDOMElementPtr elptr=foundNode;
strAttributeValue=_com_util::ConvertBSTRToString(_bstr_t( elptr->getAttribute(_bstr_t(cstrAttributeName))));
}
if (bstr)
{
SysFreeString(bstr);
bstr = NULL;
}
return 0;
}
else
return -1;
}
LONG CXMLFun::SetNodeValue(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName, CONST CHAR* cstrValue, CONST CHAR* cstrAttributeName, CONST CHAR* cstrAttributeValue)
{
/* RETURN VALUES:
0 = SUCCESS -1 = LOAD FAILED -2 = NODE NOT FOUND
-3 = PUT TEXT FAILED -4 = SAVE FAILED
*/
LONG lRetVal = 0;
INT iNumKeys = 0;
std::string* pCStrKeys = NULL;
// Add the value to the base key separated by a '\'
std::string strBaseKeyName(cstrBaseKeyName);
if( strBaseKeyName.at(strBaseKeyName.length() -1) !='/' )
strBaseKeyName += "/";
strBaseKeyName += cstrValueName;
// Parse all keys from the base key name (keys separated by a '\')
pCStrKeys = ParseKeys(strBaseKeyName.c_str(), iNumKeys);
// Traverse the xml using the keys parsed from the base key name to find the correct node
if (pCStrKeys)
{
if (XmlDocPtr == NULL)
return -2;
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
XmlDocPtr->get_documentElement(&rootElem); // root node
if (rootElem)
{
// returns the last node in the chain
foundNode = FindNode(rootElem, pCStrKeys, iNumKeys, true);
if (foundNode)
{
HRESULT hr=NULL;
// set the text of the node (will be the value we sent)
if(cstrValue!=NULL)
hr = foundNode->put_text(_bstr_t(cstrValue));
if(cstrAttributeName!=NULL )
{
MSXML2::IXMLDOMElementPtr elptr=foundNode;
elptr->setAttribute(_bstr_t(cstrAttributeName),
_bstr_t(cstrAttributeValue) );
}
if (!SUCCEEDED(hr))
lRetVal = -3;
foundNode = NULL;
}
else
lRetVal = -2;
rootElem = NULL;
}
delete [] pCStrKeys;
}
return lRetVal;
}
// XMLFun.DeleteSetting("Settings/who","");删除该键及其所有子键
// delete a key or chain of keys
LONG CXMLFun::DeleteSetting(CONST CHAR* cstrBaseKeyName, CONST CHAR* cstrValueName)
{
LONG bRetVal = -1;
INT iNumKeys = 0;
std::string* pCStrKeys = NULL;
std::string strBaseKeyName(cstrBaseKeyName);
if ( strBaseKeyName!="" )
{
if( strBaseKeyName.at(strBaseKeyName.length() -1) !='/' )
strBaseKeyName += "/";
strBaseKeyName += std::string(cstrValueName);
}
// Parse all keys from the base key name (keys separated by a '\')
pCStrKeys = ParseKeys(strBaseKeyName.c_str(), iNumKeys);
// Traverse the xml using the keys parsed from the base key name to find the correct node.
if (pCStrKeys)
{
if (XmlDocPtr == NULL)
return bRetVal;
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
XmlDocPtr->get_documentElement(&rootElem); // root node
if (rootElem)
{
// returns the last node in the chain
foundNode = FindNode(rootElem, pCStrKeys, iNumKeys);
if (foundNode)
{
// get the parent of the found node and use removeChild to delete the found node
MSXML2::IXMLDOMNodePtr parentNode = NULL;
foundNode->get_parentNode(&parentNode);
if (parentNode)
{
HRESULT hr = parentNode->removeChild(foundNode);
parentNode = NULL;
}
foundNode = NULL;
}
rootElem = NULL;
}
delete [] pCStrKeys;
}
return bRetVal;
}
// get a basekey's all children's value
LONG CXMLFun::GetKeysValue(CONST CHAR* cstrBaseKeyName, std::map<std::string, std::string>& keys_val)
{
INT iNumKeys = 0;
std::string* pCStrKeys = NULL;
std::string strValue;
pCStrKeys = ParseKeys(cstrBaseKeyName, iNumKeys);
if (pCStrKeys)
{
if (XmlDocPtr == NULL) // load the xml document
return -1;
MSXML2::IXMLDOMElementPtr rootElem = NULL;
MSXML2::IXMLDOMNodePtr foundNode = NULL;
MSXML2::IXMLDOMNodeListPtr nodelst= NULL;
MSXML2::IXMLDOMNodePtr pNode= NULL;
XmlDocPtr->get_documentElement(&rootElem); // root node
if (rootElem)
{
foundNode = FindNode(rootElem, pCStrKeys, iNumKeys);
if (foundNode)
nodelst=foundNode->GetchildNodes();
if(nodelst!=NULL)
{
for (INT i=0; i<nodelst->length; i++)
{
pNode = nodelst->item[i];
keys_val[(CONST CHAR*)(pNode->nodeName)]=pNode->text;//(CONST CHAR*)pNode->xml;
}
foundNode = NULL;
}
pNode=N
没有合适的资源?快使用搜索试试~ 我知道了~
VC读写XML文件,包括新建、插入、修改,读取、删除等操作
共2个文件
h:1个
cpp:1个
4星 · 超过85%的资源 需积分: 13 166 下载量 39 浏览量
2010-07-11
11:45:06
上传
评论 7
收藏 5KB RAR 举报
温馨提示
经过笔者多年编写系统经验开发,能自动建立XML文件头,根据自定义的HEAD和BODY等建立相应的表项,进行新建、插入、修改,读取、删除等操作,该CLASS已经得到了广泛的应用,对使用XML作为基本配置文件很有帮助!
资源推荐
资源详情
资源评论
收起资源包目录
VC XML.rar (2个子文件)
XMLFun.cpp 14KB
XMLFun.h 4KB
共 2 条
- 1
My_lolo
- 粉丝: 11
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页