怎样用VC++解析XML文件?(15分)

  • 主题发起人 主题发起人 rainbowfish
  • 开始时间 开始时间
R

rainbowfish

Unregistered / Unconfirmed
GUEST, unregistred user!
我已经下了一个expat parser可是我不知道怎么用,请哪位高手指点一下!谢!!!
 
自己写个XML解析类吧!使用微软的XML的解析接口就可以了。
/******************************************************************************** 文件名称: MsXml.h
********************************************************************************/
#pragma once
#include <afxtempl.h>
#import <msxml.dll> named_guids
using namespace MSXML;
class CMsXml
{
public:
CString Get_Child_Attrib( LPCTSTR szAttrib );
BOOL Go_To_Node( LPCTSTR szName);
BOOL Go_To_NextSibling();
BOOL Go_to_Parent();
BOOL Go_to_Child();
MSXML::IXMLDOMDocumentPtr m_pDOMDoc;
CMsXml();
CMsXml( LPCTSTR szDoc );
virtual ~CMsXml();
// Parse and Navigate
BOOL SetDoc( LPCTSTR szDoc );
BOOL FindElem( LPCTSTR szName=NULL );
BOOL FindChildElem( LPCTSTR szName=NULL );
BOOL IntoElem();
BOOL OutOfElem();
void ResetChildPos() { if ( m_pChild ) m_pChild.Release();
};
void ResetPos();
CString GetTagName() const { return x_GetTagName(m_pCurrent);
};
CString GetChildTagName() const { return x_GetTagName(m_pChild);
};
CString GetData() const { return x_GetData( m_pCurrent );
};
CString GetChildData() const { return x_GetData(m_pChild);
};
CString GetAttrib( LPCTSTR szAttrib ) const { return x_GetAttrib( m_pCurrent, szAttrib );
};
CString GetChildAttrib( LPCTSTR szAttrib ) const { return x_GetAttrib( m_pChild, szAttrib );
};
CString GetCDATA();
// Create
BOOL AddElem( LPCTSTR szName, LPCTSTR szData=NULL );
BOOL InsertChildElem( LPCTSTR szName, LPCTSTR szData=NULL );
BOOL AddChildElem( LPCTSTR szName, LPCTSTR szData=NULL );
BOOL AddAttrib( LPCTSTR szAttrib, LPCTSTR szValue );
BOOL AddChildAttrib( LPCTSTR szAttrib, LPCTSTR szValue );
BOOL AddAttrib( LPCTSTR szAttrib, int nValue );
BOOL AddChildAttrib( LPCTSTR szAttrib, int nValue );
BOOL AddCDATA( LPCTSTR szData );
CString GetDoc() const;
BOOL AddChildSubDoc( LPCTSTR szSubDoc );
BOOL InsertChildSubDoc( LPCTSTR szSubDoc );
CString GetChildSubDoc() const;
// Modify
BOOL RemoveElem();
BOOL RemoveChildElem();
BOOL SetAttrib( LPCTSTR szAttrib, LPCTSTR szValue );
BOOL SetChildAttrib( LPCTSTR szAttrib, LPCTSTR szValue );
BOOL SetAttrib( LPCTSTR szAttrib, int nValue );
BOOL SetChildAttrib( LPCTSTR szAttrib, int nValue );
BOOL SetData( LPCTSTR szData ) { return x_SetData(m_pCurrent,szData);
};
BOOL SetChildData( LPCTSTR szData ) { return x_SetData(m_pChild,szData);
};
protected:
MSXML::IXMLDOMNodePtr m_pCurrent;
MSXML::IXMLDOMNodePtr m_pChild;
CString x_GetTagName( MSXML::IXMLDOMNodePtr pNode ) const;
CString x_GetData( MSXML::IXMLDOMNodePtr pNode ) const;
CString x_GetAttrib( MSXML::IXMLDOMNodePtr pNode, LPCTSTR szAttrib ) const;
MSXML::IXMLDOMNodePtr x_Insert( MSXML::IXMLDOMNodePtr pParent, MSXML::IXMLDOMNodePtr pBefore, LPCTSTR szName, LPCTSTR szData );
MSXML::IXMLDOMNodePtr x_InsertSubDoc( MSXML::IXMLDOMNodePtr pParent, MSXML::IXMLDOMNodePtr pBefore, LPCTSTR szSubDoc );
BOOL x_SetAttrib( MSXML::IXMLDOMNodePtr pNode, LPCTSTR szAttrib, LPCTSTR szValue );
BOOL x_SetData( MSXML::IXMLDOMNodePtr&amp;
pNode, LPCTSTR szData );
};
/******************************************************************************** 文件名称:MsXml.cpp *******************************************************************************/
#include "StdAfx.h"
#include "afxconv.h"
#include "msxml.h"
CMsXml::CMsXml()
{
CoInitialize(NULL);
m_pDOMDoc.CreateInstance(MSXML::CLSID_DOMDocument);
}
CMsXml::CMsXml( LPCTSTR szDoc )
{
CoInitialize(NULL);
m_pDOMDoc.CreateInstance(MSXML::CLSID_DOMDocument);
SetDoc( szDoc );
}
void CMsXml::ResetPos()
{
if ( m_pCurrent )
m_pCurrent.Release();
if ( m_pChild )
m_pChild.Release();
};
BOOL CMsXml::SetDoc( LPCTSTR szDoc )
{
USES_CONVERSION;
_bstr_t bstrDoc(A2BSTR(szDoc));
VARIANT_BOOL bResult = m_pDOMDoc->loadXML( bstrDoc );
ResetPos();
return bResult;
};
CString CMsXml::GetDoc() const
{
return (LPCTSTR)m_pDOMDoc->xml;
};
CMsXml::~CMsXml()
{
}
BOOL CMsXml::FindElem( LPCTSTR szName )
{
MSXML::IXMLDOMNodePtr pNode = m_pCurrent;
if ( pNode )
pNode = pNode->GetnextSibling();
else
pNode = m_pDOMDoc->GetfirstChild();
while ( pNode )
{
if ( pNode->nodeType == MSXML::NODE_ELEMENT )
{
// Compare tag name unless szName is not specified
if ( szName == NULL || !szName[0] || x_GetTagName(pNode) == szName )
{
// Assign new position
m_pCurrent = pNode;
if ( m_pChild )
m_pChild.Release();
return TRUE;
}
}
pNode = pNode->GetnextSibling();
}
return FALSE;
}
BOOL CMsXml::FindChildElem( LPCTSTR szName )
{
if ( ! ((bool)m_pCurrent) )
FindElem();
if ( ! ((bool)m_pCurrent) )
return FALSE;

MSXML::IXMLDOMNodePtr pNode = m_pChild;

if ( pNode )
pNode = pNode->GetnextSibling();
else
pNode = m_pCurrent->GetfirstChild();
while ( pNode )
{
if ( pNode->nodeType == MSXML::NODE_ELEMENT )
{
// Compare tag name unless szName is not specified
if ( szName == NULL || !szName[0] || x_GetTagName(pNode) == szName )
{
// Assign new position
m_pChild = pNode;
return TRUE;
}
}
pNode = pNode->GetnextSibling();
}
return FALSE;
}
BOOL CMsXml::IntoElem()
{
if ( ! ((bool)m_pChild) )
FindChildElem();
if ( m_pChild )
{
m_pCurrent = m_pChild;
if ( m_pChild )
{
m_pChild.Release();
return TRUE;
}
}
return FALSE;
}
BOOL CMsXml::OutOfElem()
{
MSXML::IXMLDOMNodePtr pNode = m_pCurrent->GetparentNode();
if ( pNode )
{
m_pChild = m_pCurrent;
m_pCurrent = pNode;
return TRUE;
}
return FALSE;
}
BOOL CMsXml::AddElem( LPCTSTR szName, LPCTSTR szData )
{
MSXML::IXMLDOMNodePtr pParent;
if ( m_pChild )
m_pChild.Release();
if ( m_pCurrent )
{
pParent = m_pCurrent->GetparentNode();
m_pCurrent = m_pCurrent->GetnextSibling();
}
else
pParent = m_pDOMDoc;
m_pCurrent = x_Insert( pParent, m_pCurrent, szName, szData );
if ( m_pCurrent )
return TRUE;
return FALSE;
}
BOOL CMsXml::AddChildElem( LPCTSTR szName, LPCTSTR szData )
{
if ( ! (bool)m_pCurrent )
return FALSE;
if ( m_pChild )
m_pChild = m_pChild->GetnextSibling();
m_pChild = x_Insert( m_pCurrent, m_pChild, szName, szData );
if ( m_pChild )
return TRUE;
return FALSE;
}
BOOL CMsXml::InsertChildElem( LPCTSTR szName, LPCTSTR szData )
{
if ( ! (bool)m_pCurrent )
return FALSE;
if ( ! (bool)m_pChild )
m_pChild = m_pCurrent->GetfirstChild();
m_pChild = x_Insert( m_pCurrent, m_pChild, szName, szData );
if ( m_pChild )
return TRUE;
return FALSE;
}
BOOL CMsXml::AddAttrib( LPCTSTR szAttrib, int nValue )
{
_TCHAR szVal[25];
_itot( nValue, szVal, 10 );
return AddAttrib( szAttrib, szVal );
}
BOOL CMsXml::AddAttrib( LPCTSTR szAttrib, LPCTSTR szValue )
{
if ( m_pCurrent )
return x_SetAttrib( m_pCurrent, szAttrib, szValue );
return FALSE;
}
BOOL CMsXml::AddChildAttrib( LPCTSTR szAttrib, int nValue )
{
_TCHAR szVal[25];
_itot( nValue, szVal, 10 );
return AddChildAttrib( szAttrib, szVal );
}
BOOL CMsXml::AddChildAttrib( LPCTSTR szAttrib, LPCTSTR szValue )
{
if ( m_pChild )
return x_SetAttrib( m_pChild, szAttrib, szValue );
return FALSE;
}
BOOL CMsXml::AddCDATA( LPCTSTR szData )
{
MSXML::IXMLDOMCDATASectionPtr pData = m_pDOMDoc->createCDATASection( _bstr_t(szData) );
if ( pData )
{
m_pChild = pData;
m_pCurrent->appendChild( pData );
return TRUE;
}
return FALSE;
}
CString CMsXml::GetCDATA()
{
ResetChildPos();
if ( FindChildElem( _T("#cdata-section") ) )
{
MSXML::IXMLDOMCDATASectionPtr pData = m_pChild;
return (LPCTSTR)pData->Gettext();
}
return _T("");
}
BOOL CMsXml::AddChildSubDoc( LPCTSTR szSubDoc )
{
if ( ! (bool)m_pCurrent )
return FALSE;
if ( m_pChild )
m_pChild = m_pChild->GetnextSibling();
m_pChild = x_InsertSubDoc( m_pCurrent, m_pChild, szSubDoc );
if ( m_pChild )
return TRUE;
return FALSE;
}
BOOL CMsXml::InsertChildSubDoc( LPCTSTR szSubDoc )
{
if ( ! (bool)m_pCurrent )
return FALSE;
if ( ! (bool)m_pChild )
m_pChild = m_pCurrent->GetfirstChild();
m_pChild = x_InsertSubDoc( m_pCurrent, m_pChild, szSubDoc );
if ( m_pChild )
return TRUE;
return FALSE;
}
CString CMsXml::GetChildSubDoc() const
{
if ( (bool)m_pChild )
{
return (LPCTSTR)m_pChild->xml;
}
return "";
}
BOOL CMsXml::RemoveElem()
{
if ( m_pCurrent )
{
MSXML::IXMLDOMNodePtr pParent = m_pCurrent->GetparentNode();
if ( pParent )
{
if ( m_pChild )
m_pChild.Release();
MSXML::IXMLDOMNodePtr pPrev = m_pCurrent->GetpreviousSibling();
pParent->removeChild( m_pCurrent );
m_pCurrent = pPrev;
return TRUE;
}
}
return FALSE;
}
BOOL CMsXml::RemoveChildElem()
{
if ( m_pChild )
{
MSXML::IXMLDOMNodePtr pPrev = m_pChild->GetpreviousSibling();
m_pCurrent->removeChild( m_pChild );
m_pChild.Release();
m_pChild = pPrev;
return TRUE;
}
return FALSE;
}
BOOL CMsXml::SetAttrib( LPCTSTR szAttrib, int nValue )
{
_TCHAR szVal[25];
_itot( nValue, szVal, 10 );
return SetAttrib( szAttrib, szVal );
}
BOOL CMsXml::SetAttrib( LPCTSTR szAttrib, LPCTSTR szValue )
{
if ( m_pCurrent )
return x_SetAttrib( m_pCurrent, szAttrib, szValue );
return FALSE;
}
BOOL CMsXml::SetChildAttrib( LPCTSTR szAttrib, int nValue )
{
_TCHAR szVal[25];
_itot( nValue, szVal, 10 );
return SetChildAttrib( szAttrib, szVal );
}
BOOL CMsXml::SetChildAttrib( LPCTSTR szAttrib, LPCTSTR szValue )
{
if ( m_pChild )
return x_SetAttrib( m_pChild, szAttrib, szValue );
return FALSE;
}
//////////////////////////////////////////////////////////////////////
// Private Methods
//////////////////////////////////////////////////////////////////////
CString CMsXml::x_GetTagName( MSXML::IXMLDOMNodePtr pNode ) const
{
CString csTagName = (LPCTSTR)pNode->GetnodeName();
return csTagName;
}
CString CMsXml::x_GetData( MSXML::IXMLDOMNodePtr pNode ) const
{
return (LPCTSTR) pNode->Gettext();
}
CString CMsXml::x_GetAttrib( MSXML::IXMLDOMNodePtr pNode, LPCTSTR szAttrib ) const
{
CString csAttrib;
MSXML::IXMLDOMNodePtr pAttrib;
HRESULT hr = pNode->Getattributes()->raw_getNamedItem( _bstr_t(szAttrib), &amp;pAttrib );
if ( SUCCEEDED(hr) &amp;&amp;
((bool)pAttrib) )
{
_variant_t varVal = pAttrib->GetnodeValue();
if ( varVal.vt == VT_BSTR )
csAttrib = (LPCTSTR)_bstr_t(varVal.bstrVal);
}
return csAttrib;
}
MSXML::IXMLDOMNodePtr CMsXml::x_Insert( MSXML::IXMLDOMNodePtr pParent, MSXML::IXMLDOMNodePtr pBefore, LPCTSTR szName, LPCTSTR szData )
{
MSXML::IXMLDOMElementPtr pNew = m_pDOMDoc->createElement( _bstr_t(szName) );
VARIANT varRef;
VariantInit( &amp;varRef );
varRef.vt = VT_DISPATCH;
varRef.pdispVal = pBefore.GetInterfacePtr();
pParent->insertBefore( pNew, varRef );
if ( szData &amp;&amp;
szData[0] )
{
MSXML::IXMLDOMNodePtr pText = m_pDOMDoc->createTextNode( _bstr_t(szData) );
pNew->appendChild( pText );
}
return pNew;
}
MSXML::IXMLDOMNodePtr CMsXml::x_InsertSubDoc( MSXML::IXMLDOMNodePtr pParent, MSXML::IXMLDOMNodePtr pBefore, LPCTSTR szSubDoc )
{
MSXML::IXMLDOMDocumentPtr pSubDoc;
pSubDoc.CreateInstance( _T("Microsoft.XMLDOM") );
_bstr_t bstrSubDoc(A2BSTR(szSubDoc));
VARIANT_BOOL bResult = pSubDoc->loadXML( bstrSubDoc );
if ( ! bResult )
return NULL;
MSXML::IXMLDOMElementPtr pNew = pSubDoc->GetdocumentElement();
VARIANT varRef;
VariantInit( &amp;varRef );
varRef.vt = VT_DISPATCH;
varRef.pdispVal = pBefore.GetInterfacePtr();
pParent->insertBefore( pNew, varRef );
return pNew;
}
BOOL CMsXml::x_SetAttrib( MSXML::IXMLDOMNodePtr pNode, LPCTSTR szAttrib, LPCTSTR szValue )
{
MSXML::IXMLDOMNamedNodeMapPtr pAttribs = pNode->Getattributes();
MSXML::IXMLDOMAttributePtr pAttr = m_pDOMDoc->createAttribute( _bstr_t(szAttrib) );
if ( pAttr )
{
_variant_t varVal;
varVal.SetString(szValue);
pAttr->put_value( varVal );
pAttribs->setNamedItem( pAttr );
return TRUE;
}
return FALSE;
}
BOOL CMsXml::x_SetData( MSXML::IXMLDOMNodePtr&amp;
pNode, LPCTSTR szData )
{
MSXML::IXMLDOMNodePtr pChild = pNode->GetfirstChild();
if ( (bool)pChild &amp;&amp;
pChild->nodeType != MSXML::NODE_TEXT )
return FALSE;
MSXML::IXMLDOMNodePtr pText = m_pDOMDoc->createTextNode( _bstr_t(szData) );
if ( (bool)pChild )
{
pNode->replaceChild( pText, pChild );
}
else
{
pNode->appendChild( pText );
}
return TRUE;
}
//第一个子结点
BOOL CMsXml::Go_to_Child()
{
if ( ! ((bool)m_pCurrent) )
FindElem();
if ( ! ((bool)m_pCurrent) )
return FALSE;
else
{
m_pCurrent = m_pCurrent->GetfirstChild();
return true;
}
}
//父结点
BOOL CMsXml::Go_to_Parent()
{
if ( ! ((bool)m_pCurrent) )
FindElem();
if ( ! ((bool)m_pCurrent) )
return FALSE;
else
{
m_pCurrent = m_pCurrent->GetparentNode();

return true;
}
}
//下一个
BOOL CMsXml::Go_To_NextSibling()
{
if ( ! ((bool)m_pCurrent) )
return FALSE;
else
{
m_pCurrent = m_pCurrent->GetnextSibling();

return (m_pCurrent!= NULL);
}
}
//
BOOL CMsXml::Go_To_Node(LPCTSTR szName)
{
//ResetPos();
MSXML::IXMLDOMNodePtr pNode = m_pCurrent;
if ( pNode)
//pNode = pNode->GetnextSibling()
;
else
pNode = m_pDOMDoc->GetfirstChild();
while ( pNode )
{
if ( pNode->nodeType == MSXML::NODE_ELEMENT )
{
// Compare tag name unless szName is not specified
if ( szName == NULL || !szName[0] || x_GetTagName(pNode) == szName )
{
// Assign new position
m_pCurrent = pNode;
if ( m_pChild )
m_pChild.Release();
return TRUE;
}
}
pNode = pNode->GetnextSibling();
}
return FALSE;
}
CString CMsXml::Get_Child_Attrib(LPCTSTR szAttrib)
{
MSXML::IXMLDOMNodePtr pNode = m_pCurrent;
if ( ! ((bool)pNode) )
return "";
else
{
pNode = pNode->GetfirstChild();
if (pNode!=NULL)
return x_GetAttrib( pNode, szAttrib );
else
return "";
}
}
 
后退
顶部