本命年沒穿紅內褲,大年初一被狗咬,慘,所以大家本命年切記要穿,否則會倒霉的!!!
去年年底寫一個配置文件(XML格式),第一次直接用MSXML,勉強實現,代碼冗余且雜亂,剛寫好,需求變了些,代碼很難維護,所以自己寫了一個封裝類,接口層都是STL類,用起來比較方便,這樣可以剩了類型轉換的麻煩,適合不熟悉COM的程序員。只封裝了部分功能,但是XML基本操作都可以實現。
目的:為了更方便的讀寫XML文檔,對MSXML4.0類進行封裝
主要解決一些接口參數轉換問題
使用前確保已經安裝好MSXML4.0且設置好環境
適合VC開發語言
下面是類:
class WLWXML
{
public:
WLWXML()
{
m_pIXMLDoc = NULL;
}
~WLWXML()
{
SafeReleaseXMLDoc();
}
// 創建一個XML文檔,成功返回true,失敗返回false
bool ConstructXMLFile();
// 從文件加載一個XML文件,加載成功返回true,加載失敗返回false
bool LoadFromXMLFile(const std::string& fileName);
// 保存XML文件到fileName,成功返回true,失敗返回false
bool SaveToXMLFile(const std::string& fileName);
// 安全釋放XML文檔
void SafeReleaseXMLDoc();
// 獲得XML文件內容
void GetXML(std::string& strXML);
// 在文檔pIParentElem元素下添加nodeName節點,值為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節點的值
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);
// 獲得節點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();
// 獲得節點的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
萬連文 閱讀(3135)
評論(5) 編輯 收藏 引用 所屬分類:
亂七八糟