# Pragma once
//--------------------------------------------------------//
# Import <msxml6.dll>
# Import "MSXML. dll"
Using namespace MSXML;
//--------------------------------------------------------//
Class cmyxml
{
Public:
Cmyxml (void );
Public:
~ Cmyxml (void );
Public:
MSXML: ixmldomdocumentptr xmldoc;
Int loadxmlstr (cstring Str );
Int loadxmlfile (cstring filename );
Protected:
Int Init (void );
Public:
Int createnode (cstring nodepath, cstring nodename );
Int createnode (cstring nodepath, cstring nodename, cstring value );
Int resetnodevalue (cstring nodepath, cstring value );
Int deltenode (cstring parentnodepath, cstring nodepath );
Cstring getxml (void );
Cstring getnodevalue (cstring nodepath );
Cstring getattribute (cstring nodelistname, cstring attributename );
// Cstring getattribute (cstring nodelistname, cstring attributename1, cstring attributename2 );
// Ixmldomnodelist getnodelist (char * elementsbytagname );
};
------------------------------------------------------------
# Include "stdafx. H"
# Include "myxml. H"
Cmyxml: cmyxml (void)
{
}
Cmyxml ::~ Cmyxml (void)
{
Xmldoc = NULL;
}
Int cmyxml: Init (void)
{
Hresult hR =: coinitialize (null); // initialization of COM
If (! Succeeded (HR ))
Return-1;
Xmldoc. createinstance (_ uuidof (MSXML: domdocument ));
Return 0;
}
Int cmyxml: loadxmlstr (cstring Str)
{
If (Init ())
{
Return-1;
}
If (! Xmldoc-> loadxml (_ bstr_t (STR )))
{
Return-1;
}
Return 0;
}
Int cmyxml: loadxmlfile (cstring filename)
{
If (Init ())
{
Return-1;
}
If (! Xmldoc-> load (_ bstr_t) filename ))
{
Return-1;
}
Return 0;
}
Cstring cmyxml: getxml (void)
{
// Return xmldoc-> gettext ();
Cstring STR = xmldoc-> gettext ();
Return STR;
}
Int cmyxml: createnode (cstring nodepath, cstring nodename)
{
Try
{
MSXML: ixmldomnodeptr rootnode = NULL;
MSXML: ixmldomnodeptr newnode = NULL;
Rootnode = xmldoc-> selectsinglenode (_ bstr_t) nodepath );
Newnode = xmldoc-> createnode (MSXML: node_document_type, (_ bstr_t) nodename ,"");
Rootnode-> appendchild (newnode );
} Catch (...)
{
}
Return 0;
}
Int cmyxml: createnode (cstring nodepath, cstring nodename, cstring value)
{
Try
{
MSXML: ixmldomnodeptr rootnode = NULL;
MSXML: ixmldomnodeptr newnode = NULL;
Rootnode = xmldoc-> selectsinglenode (_ bstr_t) nodepath );
Newnode = xmldoc-> createnode (MSXML: node_document_type, (_ bstr_t) nodename ,"");
Newnode-> nodevalue = (_ bstr_t) value ;//
Rootnode-> appendchild (newnode );
} Catch (...)
{
}
Return 0;
}
Int cmyxml: resetnodevalue (cstring nodepath, cstring value)
{
Try
{
MSXML: ixmldomnodeptr rootnode = NULL;
Rootnode = xmldoc-> selectsinglenode (_ bstr_t) nodepath );
Rootnode-> nodevalue = (_ bstr_t) value ;//
} Catch (...)
{
}
Return 0;
}
Int cmyxml: deltenode (cstring parentnodepath, cstring nodepath)
{
Try
{
MSXML: ixmldomnodeptr rootnode = NULL;
MSXML: ixmldomnodeptr deletenode = NULL;
Rootnode = xmldoc-> selectsinglenode (_ bstr_t) parentnodepath );
Deletenode = xmldoc-> selectsinglenode (_ bstr_t) nodepath );
Rootnode-> removechild (deletenode );
} Catch (...)
{
}
Return 0;
}
Cstring cmyxml: getnodevalue (cstring nodepath)
{
Try
{
Cstring STR = xmldoc-> selectsinglenode (_ bstr_t) nodepath)-> gettext (); // return xmldoc-> selectsinglenode (_ bstr_t) nodepath)-> gettext ();
Return STR;
} Catch (...) // catch (cexception * Ex)
{
}
Return 0;
}
Cstring cmyxml: getattribute (cstring nodelistname, cstring attributename)
{
MSXML: ixmldomnode * domnode = NULL;
Try
{
MSXML: ixmldomnodelistptr nodelist = xmldoc-> getelementsbytagname (_ bstr_t) nodelistname );
Cstring sattributename;
For (INT I = 0; I <nodelist-> length; I ++)
{
Nodelist-> get_item (I, & domnode );
BSTR battributename = domnode-> getattributes ()-> getnameditem (_ bstr_t) attributename)-> gettext (); // strcontest = (lpcstr) domnode-> text;
Sattributename = battributename;
Return sattributename;
}
}
Catch (...)
{
}
Delete domnode;
Return 0;
}
// Cstring cmyxml: getattribute (cstring nodelistname, cstring attributename1, cstring attributename2)
//{
// Try
//{
// MSXML: ixmldomnodelistptr nodelist = xmldoc-> getelementsbytagname (_ bstr_t) nodelistname );
// MSXML: ixmldomnode * domnode = NULL;
// Cstring strcontest;
// For (INT I = 0; I <nodelist-> length; I ++)
//{
// Nodelist-> get_item (I, & domnode );
// BSTR m_type = domnode-> getattributes ()-> getnameditem (_ bstr_t) attributename1)-> gettext ();
// Strcontest = m_type;
// Strcontest = (lpcstr) domnode-> text;
// Afxmessagebox (strcontest );
//}
//}
// Catch (_ com_error & ERR)
//{
// Cstring strerr = (lpctstr) Err. Description ();
// Afxmessagebox (strerr );
//}
// Return 0;
//}
// Tchar szerror [1024];
// E-> geterrormessage (szerror, 1024); // E. geterrormessage (szerror, 1024 );
//: Afxmessagebox (szerror );
// Deletion exception
// E-> Delete ();
// For (INT I = 0; I <nodelist-> length; I ++)
//{
// Nodelist-> get_item (I, & domnode );
// BSTR m_type = domnode-> getattributes ()-> getnameditem (_ bstr_t) nodename)-> gettext ();
// Strcontest = (lpcstr) domnode-> text;
// Afxmessagebox (strcontest );
//}