本命年沒(méi)穿紅內(nèi)褲,大年初一被狗咬,慘,所以大家本命年切記要穿,否則會(huì)倒霉的!!!
去年年底寫一個(gè)配置文件(XML格式),第一次直接用MSXML,勉強(qiáng)實(shí)現(xiàn),代碼冗余且雜亂,剛寫好,需求變了些,代碼很難維護(hù),所以自己寫了一個(gè)封裝類,接口層都是STL類,用起來(lái)比較方便,這樣可以剩了類型轉(zhuǎn)換的麻煩,適合不熟悉COM的程序員。只封裝了部分功能,但是XML基本操作都可以實(shí)現(xiàn)。
目的:為了更方便的讀寫XML文檔,對(duì)MSXML4.0類進(jìn)行封裝
主要解決一些接口參數(shù)轉(zhuǎn)換問(wèn)題
使用前確保已經(jīng)安裝好MSXML4.0且設(shè)置好環(huán)境
適合VC開(kāi)發(fā)語(yǔ)言
下面是類:
class WLWXML
{
public:
WLWXML()
{
m_pIXMLDoc = NULL;
}
~WLWXML()
{
SafeReleaseXMLDoc();
}
// 創(chuàng)建一個(gè)XML文檔,成功返回true,失敗返回false
bool ConstructXMLFile();
// 從文件加載一個(gè)XML文件,加載成功返回true,加載失敗返回false
bool LoadFromXMLFile(const std::string& fileName);
// 保存XML文件到fileName,成功返回true,失敗返回false
bool SaveToXMLFile(const std::string& fileName);
// 安全釋放XML文檔
void SafeReleaseXMLDoc();
// 獲得XML文件內(nèi)容
void GetXML(std::string& strXML);
// 在文檔pIParentElem元素下添加nodeName節(jié)點(diǎn),值為nodeValue
bool AppendMemberNode( const std::string& nodeName,
const std::string& nodeValue,
IXMLDOMElement* pIParentElem,
IXMLDOMNode** ppOutNewChild=NULL);
bool AppendMemberNode( const std::string& nodeName,
int nodeValue,
IXMLDOMElement* pIParentElem,
IXMLDOMNode** ppOutNewChild=NULL);
bool AppendMemberNode( const std::string& nodeName,
long nodeValue,
IXMLDOMElement* pIParentElem,
IXMLDOMNode** ppOutNewChild=NULL);
bool AppendMemberNode( const std::string& nodeName,
double nodeValue,
IXMLDOMElement* pIParentElem,
IXMLDOMNode** ppOutNewChild=NULL);
bool AppendMemberNode( const std::string& nodeName,
bool nodeValue,
IXMLDOMElement* pIParentElem,
IXMLDOMNode** ppOutNewChild=NULL);
// 為元素pIParentElem添加屬性
bool AppendAttributeNode(const std::string& nodeName,
const std::string& nodeValue,
IXMLDOMElement* pIParentElem);
bool AppendAttributeNode(const std::string& nodeName,
int nodeValue,
IXMLDOMElement* pIParentElem);
bool AppendAttributeNode(const std::string& nodeName,
long nodeValue,
IXMLDOMElement* pIParentElem);
bool AppendAttributeNode(const std::string& nodeName,
double nodeValue,
IXMLDOMElement* pIParentElem);
bool AppendAttributeNode(const std::string& nodeName,
bool nodeValue,
IXMLDOMElement* pIParentElem);
// 獲取pIParentElem元素下nodeName節(jié)點(diǎn)的值
bool GetNodeValue( IXMLDOMNode* pIParentElem,
const std::string& nodeName,
std::string& nodeValue);
bool GetNodeValue( IXMLDOMNode* pIParentElem,
const std::string& nodeName,
int& nodeValue);
bool GetNodeValue( IXMLDOMNode* pIParentElem,
const std::string& nodeName,
long& nodeValue);
bool GetNodeValue( IXMLDOMNode* pIParentElem,
const std::string& nodeName,
double& nodeValue);
bool GetNodeValue( IXMLDOMNode* pIParentElem,
const std::string& nodeName,
bool& nodeValue);
// 獲得節(jié)點(diǎn)pIParentElem的屬性
bool GetAttributeNode(IXMLDOMNode* pIParentElem,
const std::string& nodeName,
std::string& nodeValue);
bool GetAttributeNode(IXMLDOMNode* pIParentElem,
const std::string& nodeName,
int& nodeValue);
bool GetAttributeNode(IXMLDOMNode* pIParentElem,
const std::string& nodeName,
long& nodeValue);
bool GetAttributeNode(IXMLDOMNode* pIParentElem,
const std::string& nodeName,
double& nodeValue);
bool GetAttributeNode(IXMLDOMNode* pIParentElem,
const std::string& nodeName,
bool& nodeValue);
// 獲得文檔元素
IXMLDOMElement* GetDocElem();
// 獲得節(jié)點(diǎn)的nodeName孩子
IXMLDOMNode* GetChildNode(IXMLDOMNode* pIParentElem,
const std::string& nodeName,
std::string& nodeValue);
protected:
private:
IXMLDOMDocument2* m_pIXMLDoc; // XML文檔
};
類以及示例下載
posted on 2006-02-10 19:38
萬(wàn)連文 閱讀(3138)
評(píng)論(5) 編輯 收藏 引用 所屬分類:
亂七八糟