http://www.ezikao.com.cn/art.view.asp?artid=5291
工作中经常要处理xml文件,每次都调用com那些对象很麻烦。而且大同小异,无非是复制原来的代码,再改改。于是有空的时候,把那些对象封装了一下,用起来舒服多了。
XML是什么东东我就不解释了
//XmlProcessor.h
#if !defined(AFX_XMLPROCESSOR_H__71FF307D_52F3_496D_B8F2_0E1A5449191F__INCLUDED_)
#define AFX_XMLPROCESSOR_H__71FF307D_52F3_496D_B8F2_0E1A5449191F__INCLUDED_
//上面那段预定义,在VC.net里面可以用: #pragma once
#import "msxml4.dll"
using namespace MSXML2;//此命名空间在VC.net下面会有冲突,需要重命名
class CXmlProcessor
{
public:
CXmlProcessor(void);
~CXmlProcessor(void);
public:
BOOL Open( LPCTSTR lpszPathName );
void Save( LPCTSTR lpszPathName ){ m_pXmlDoc->save( lpszPathName ); }
int Select ( LPCTSTR lpszNoteName );
void InsertNode ( LPCTSTR lpszNoteName);
void InsertElement ( LPCTSTR lpszEleName );
void SetAttribute ( LPCTSTR lpszAttrName, LPCTSTR lpszAttrValue )//设置节点属性
{
m_pChildNode->setAttribute( (_bstr_t)lpszAttrName, (_bstr_t)lpszAttrValue );
}
void SetNodeTxt ( LPCTSTR lpszNodeTxt ){ m_pChildNode ->Puttext((_bstr_t) lpszNodeTxt ); } // 设置字段文本值
void DeleteNode ( LPCTSTR lpszNodeName );
CString GetNodeValue ( int nNum);
CString GetNodeName( int nNum );
CString GetAttribute ( int nAttrNum ){ //获得节点属性值,参数为属性序号
m_pAttrs->get_item( nAttrNum, &m_pAttrItem);
return (char*)(_bstr_t) m_pAttrItem -> nodeTypedValue;
}
private:
IXMLDOMDocumentPtr m_pXmlDoc;
IXMLDOMElementPtr m_pXmlEle;
IXMLDOMElementPtr m_pChildNode;
IXMLDOMNamedNodeMapPtr m_pAttrs;
IXMLDOMNodePtr m_pAttrItem;
IXMLDOMNodeListPtr m_pItemlist;
};
//XmlProcessor.cpp
#include "StdAfx.h"
#include ".\xmlprocess.h"
#include "upgrade.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CXmlProcessor::CXmlProcessor(void)
{
HRESULT hr = m_pXmlDoc.CreateInstance(__uuidof(DOMDocument30));
if(!SUCCEEDED(hr))
{
AfxMessageBox("无法创建DOMDocument对象,请检查是否安装了MS XML Parser 运行库!");
}
}
CXmlProcessor::~CXmlProcessor(void)
{
}
BOOL CXmlProcessor::Open( LPCTSTR lpszPathName )
{
HRESULT hr = m_pXmlDoc->load( lpszPathName );
if ( SUCCEEDED(hr) )
return true;
else
return false;
}
int CXmlProcessor::Select( LPCTSTR lpszNoteName )//按节点名查找
{
m_pXmlEle = (IXMLDOMElementPtr)( m_pXmlDoc->selectSingleNode( lpszNoteName ) );
m_pItemlist = m_pXmlEle->GetchildNodes();
int nCount = m_pItemlist->length;
return nCount;
}
void CXmlProcessor::InsertNode ( LPCTSTR lpszNoteName )//插入一个没有子节点的节点(字段)
{
if ( m_pXmlEle == NULL )
{
m_pXmlEle = m_pXmlDoc->createElement( (_bstr_t)(char*) lpszNoteName );
m_pXmlDoc->appendChild( m_pXmlEle );
}
else
{
m_pChildNode = m_pXmlDoc->createElement( (_bstr_t)(char*) lpszNoteName );
m_pXmlEle->appendChild( m_pChildNode );
}
}
void CXmlProcessor::InsertElement( LPCTSTR lpszEleName )//插入一个拥有子节点的节点
{
m_pChildNode = m_pXmlDoc->createElement( (_bstr_t)(char*) lpszEleName );
m_pXmlEle->appendChild( m_pChildNode );
int nCount = m_pItemlist->length;
m_pXmlEle = m_pItemlist->item[ nCount - 1 ];
}
void CXmlProcessor::DeleteNode( LPCTSTR lpszNodeName ) //删除Select函数查找到的节点下(或跟节点下)所有指定的子节点
{
IXMLDOMSelectionPtr xmlSel;
xmlSel = m_pXmlDoc->selectNodes( lpszNodeName );
xmlSel->removeAll();
}
CString CXmlProcessor::GetNodeName( int nNum ) //返回节点名
{
m_pChildNode = m_pItemlist->item[ nNum ];
m_pChildNode->get_attributes(&m_pAttrs);
return (char*)(_bstr_t)m_pChildNode->nodeName;
}
CString CXmlProcessor::GetNodeName( int nNum ) //返回节点值
{
m_pChildNode = m_pItemlist->item[ nNum ];
m_pChildNode->get_attributes(&m_pAttrs);
return (char*)(_bstr_t)m_pChildNode->nodeTypedValue;
}
No comments:
Post a Comment