简单使用MSXML生成xml文件

//.h

#pragma once

#include <tchar.h>

#include <comutil.h>

#include <msxml6.h>

class CXmlDoc

{

    TCHAR  m_szXmlFile[MAX_PATH];

protected:

    IXMLDOMDocument * m_pXmlDoc;

 

public:

    CXmlDoc(void);

public:

    ~CXmlDoc(void);

 

public:

    HRESULT CreateXmlDoc();

    //读数据

    HRESULT Open(const TCHAR * pszXmlFile);

    HRESULT GetRoot(IXMLDOMElement **ppRoot);

    //写数据

    HRESULT AddDeclaration(TCHAR *version=_T("1.0"),TCHAR *encoding=_T("Unicode"));

    HRESULT CreateElement (TCHAR* tagName,IXMLDOMElement **pElement);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,TCHAR* value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,int value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,__int64 value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,float value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,double value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,short value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,bool value);

    HRESULT SetAttribute(IXMLDOMElement *pElement,TCHAR* name,BYTE value);

    HRESULT AppendChild(IXMLDOMNode* pParentNode,IXMLDOMNode* pNewChild,IXMLDOMNode **outNewChild);

    HRESULT Save(const TCHAR * pszXmlFile=NULL);

};

 

 

//cpp

#include "StdAfx.h"

#include "XmlDoc.h"

#include <Shlwapi.h>

#include <strsafe.h>

#pragma comment(lib,"msxml2.lib")

#pragma comment(lib,"comsuppw.lib")

#pragma comment(lib,"Shlwapi.lib")

#pragma comment(lib,"strsafe.lib")

CXmlDoc::CXmlDoc(void)

{

    memset(m_szXmlFile,0,sizeof(m_szXmlFile));

    m_pXmlDoc = NULL;

    //InitXmlDoc();

}

 

CXmlDoc::~CXmlDoc(void)

{

    if (m_pXmlDoc)

    {

       m_pXmlDoc->Release();

    }

}

 

HRESULT CXmlDoc::CreateXmlDoc()

{

    return CoCreateInstance(CLSID_DOMDocument60, NULL, CLSCTX_INPROC_SERVER,

       IID_IXMLDOMDocument, (void**)&m_pXmlDoc);

}

HRESULT CXmlDoc::Open(const TCHAR * pszXmlFile)

{

    VARIANT var;

    var.vt = VT_BSTR;

    var.bstrVal = _bstr_t(pszXmlFile);

    VARIANT_BOOL bSuccess = FALSE;

    return m_pXmlDoc->load(var,&bSuccess);

}

HRESULT CXmlDoc::GetRoot(IXMLDOMElement **ppRoot)

{

    return m_pXmlDoc->get_documentElement(ppRoot);

}

HRESULT CXmlDoc::Save(const TCHAR * pszXmlFile)

{

 

    VARIANT var;

   

    var.vt = VT_BSTR;

    if (!pszXmlFile&&PathFileExists(m_szXmlFile))

    {

       var.bstrVal = _bstr_t(m_szXmlFile);

    }

    else

       var.bstrVal = _bstr_t(pszXmlFile);

    return m_pXmlDoc->save(var);

}

 

HRESULT CXmlDoc::AddDeclaration(TCHAR *version,TCHAR *encoding)

{

    IXMLDOMProcessingInstruction *pi = NULL;

    TCHAR *format = _T("version=/"%s/" encoding=/"%s/"");

    TCHAR data[1024];

    memset(data,0,sizeof(data));

    StringCchPrintf(data,sizeof(data),format,version,encoding);

    HRESULT hr = m_pXmlDoc->createProcessingInstruction(_bstr_t(_T("xml")),_bstr_t(data),&pi);

    hr = m_pXmlDoc->appendChild(pi,NULL);

    pi->Release();

    return hr;

}

 

HRESULT CXmlDoc::CreateElement (TCHAR* tagName,IXMLDOMElement **pElement)

{

    return m_pXmlDoc->createElement(_bstr_t(tagName),pElement);

}

 

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,TCHAR* value)

{

    assert(pElement);

    BSTR bstrName = _bstr_t(name).copy();

    VARIANT varAttr;

    varAttr.vt = VT_BSTR;

    varAttr.bstrVal = _bstr_t(value);

   

    HRESULT hr = pElement->setAttribute(bstrName,varAttr);

    SysFreeString(bstrName);

    return hr;

 

}

 

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,int value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_INT;

    varAttr.intVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

 

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,__int64 value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_I8;

    varAttr.llVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

 

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,float value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_R4;

    varAttr.fltVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,double value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_R8;

    varAttr.dblVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,short value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_I2;

    varAttr.iVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,bool value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_BOOL;

    varAttr.boolVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

HRESULT CXmlDoc::SetAttribute(IXMLDOMElement *pElement,TCHAR* name,BYTE value)

{

    assert(pElement);

    VARIANT varAttr;

    varAttr.vt = VT_UI1;

    varAttr.bVal = value;

    return pElement->setAttribute(_bstr_t(name),varAttr);

}

HRESULT CXmlDoc::AppendChild(IXMLDOMNode* pParentNode,IXMLDOMNode* pNewChild,IXMLDOMNode **outNewChild)

{

    if (pParentNode)

    {

       return pParentNode->appendChild(pNewChild,outNewChild);

    }

    else

       return m_pXmlDoc->appendChild(pNewChild,outNewChild);

}

 

 

//demo

// XMLFile.cpp : 定义控制台应用程序的入口点。

//

 

#include "stdafx.h"

#include "XmlDoc.h"

#include <atlcomcli.h>

void EnumXmlNode(IXMLDOMNode *pNode);

int _tmain(int argc, TCHAR* argv[])

{

    CoInitialize(NULL);

    if (false)

    {

       HRESULT hr;

       CXmlDoc xml;

       xml.CreateXmlDoc();

       hr = xml.AddDeclaration();

 

       IXMLDOMElement *pProduction = NULL;

       hr = xml.CreateElement(L"Production",&pProduction);

       hr = xml.SetAttribute(pProduction,L"Name",L"Movie Story");

       hr = xml.AppendChild(NULL,pProduction,NULL);

       {

           IXMLDOMElement *pLanguate = NULL;

           hr = xml.CreateElement(L"Language",&pLanguate);

           hr = xml.SetAttribute(pLanguate,L"Name",L"English");

           hr = xml.SetAttribute(pLanguate,L"test",_T("zoo"));

           hr = xml.AppendChild(pProduction,pLanguate,NULL);

           {

              IXMLDOMElement *pHint = NULL;

              hr = xml.CreateElement(L"Hint",&pHint);

              hr = xml.SetAttribute(pHint,L"ok",_T("animal"));

              hr = xml.SetAttribute(pHint,L"id",56);

              hr = xml.SetAttribute(pHint,L"int64",1897979797987897987);

              hr = xml.SetAttribute(pHint,L"float",0.2356f);

              hr = xml.SetAttribute(pHint,L"bool",TRUE);

              pHint->put_text(_bstr_t(L"the first string"));

              hr = xml.AppendChild(pLanguate,pHint,NULL);

              pHint->Release();

           }

           pLanguate->Release();

 

           hr = xml.CreateElement(L"Language",&pLanguate);

           hr = xml.SetAttribute(pLanguate,L"Name",L"Chinese");

           hr = xml.AppendChild(pProduction,pLanguate,NULL);

           pLanguate->Release();

       }

       pProduction->Release();

       xml.Save(L"c://photo.xml");

    }

    else

    {

      

       HRESULT hr = E_FAIL;

       CXmlDoc xmlRead;

       hr = xmlRead.CreateXmlDoc();

       hr = xmlRead.Open(L"C://Users//wxt//Documents//Project 7//Project 7.xdss");

       CComPtr<IXMLDOMElement> pDOMElement=NULL;

       hr = xmlRead.GetRoot(&pDOMElement);

       EnumXmlNode(pDOMElement);

    }

    CoUninitialize();

    return 0;

}

 

void EnumXmlNode(IXMLDOMNode *pNode)

{

    DOMNodeType type;

    BSTR nodeName;

    BSTR attributeValue;

    pNode->get_nodeType(&type);

    if (type==NODE_ELEMENT)

    {

       pNode->get_nodeName(&nodeName);

       printf("<%s ",_com_util::ConvertBSTRToString(nodeName));

       CComPtr<IXMLDOMNamedNodeMap> pAttributeMap=NULL;

       pNode->get_attributes(&pAttributeMap);

       if(pAttributeMap)

       {

           LONG llen;

           pAttributeMap->get_length(&llen);

           for (int i=0;i<llen;i++)

           {

              CComPtr<IXMLDOMNode> pAttribute = NULL;

              pAttributeMap->get_item(i,&pAttribute);

              EnumXmlNode(pAttribute);

           }

           VARIANT_BOOL varHasChild;

           pNode->hasChildNodes(&varHasChild);

           if (varHasChild)

              printf(" >/n");

           else

              printf("/>/n");

 

       }

       VARIANT_BOOL varHasChild;

       pNode->hasChildNodes(&varHasChild);

       if (varHasChild)

       {

           CComPtr<IXMLDOMNodeList> pChildList=NULL;

           pNode->get_childNodes(&pChildList);

           if(pChildList)

           {

              LONG llen;

              pChildList->get_length(&llen);

              for (int i=0;i<llen;i++)

              {

                  CComPtr<IXMLDOMNode> pChildNode = NULL;

                  pChildList->get_item(i,&pChildNode);

                  EnumXmlNode(pChildNode);

              }

           }

           printf("</%s>/n",_com_util::ConvertBSTRToString(nodeName));

       }

 

    }

    else if (type==NODE_ATTRIBUTE)

    {

       pNode->get_nodeName(&nodeName);

       pNode->get_text(&attributeValue);

       printf(" %s=%s",_com_util::ConvertBSTRToString(nodeName),_com_util::ConvertBSTRToString(attributeValue));

    }

    else if (type==NODE_TEXT)

    {

       pNode->get_text(&attributeValue);

       printf(" %s ",_com_util::ConvertBSTRToString(attributeValue));

    }

}

你可能感兴趣的:(xml,null,float,byte,encoding,attributes)