// xml.cpp : implementation file
//
#include "stdafx.h"
//#include "Rems.h"
#include "xml.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// Cxml
Cxml::Cxml()
{
m_pNodeList=NULL;
m_pXmlDoc = NULL;
m_strXmlName = "";
}
Cxml::Cxml(CString strXmlFile)
{
m_pNodeList=NULL;
m_pXmlDoc = NULL;
m_strXmlName = strXmlFile;
}
Cxml::~Cxml()
{
}
CString Cxml::GetErrorMsg()
{
return m_strErrorMsg;
}
bool Cxml::Load(const char strName[])
{
::CoInitialize(NULL); //初始化COM
m_strXmlName = strName;
m_pXmlDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
HRESULT hr =m_pXmlDoc->load(strName);
if(!SUCCEEDED(hr))
{
return true;
}
else
return false;
//(CString)(char *)bstrname;
}
CString Cxml::GetSingleNodeText(const char fileName[], char nodeName[])
{
if(Load(fileName))
{
MSXML2::IXMLDOMNodePtr pNode=NULL;
bstr_t bstrname;
CString ss="//"; // "//..."表示在任意一层查找
ss = ss+nodeName;
pNode = m_pXmlDoc->selectSingleNode((_bstr_t)ss);
CString strReture;
if(pNode == NULL)
{
strReture = "0";
m_strErrorMsg = "The Node Value is Null.";
return strReture;
}
bstrname=pNode->Gettext();
//节点值
VARIANT varVal;
pNode->get_nodeTypedValue(&varVal);
CString strValue = (char*)(_bstr_t)varVal;
// strValue.Format('%s',(_bstr_t)varVal);
//节点名称
return strValue;
}
CString strTmp;
strTmp = "1";
return strTmp;
}
BOOL Cxml::UpdataSingleNodeText(char nodeName[],char nodeText[])
{
::CoInitialize(NULL); //初始化COM
MSXML2::IXMLDOMNodePtr pNode=NULL;
bstr_t bstrname;
CString ss="//"; // "//..."表示在任意一层查找
ss = ss+nodeName;
pNode = m_pXmlDoc->selectSingleNode((_bstr_t)ss);
pNode->put_text((_bstr_t)nodeText);
m_pXmlDoc->save((_variant_t)m_strXmlName);
return TRUE;
}
CString Cxml::GetSingleNodeText(char nodeName[])
{
::CoInitialize(NULL); //初始化COM
MSXML2::IXMLDOMNodePtr pNode=NULL;
bstr_t bstrname;
CString ss="//"; // "//..."表示在任意一层查找
ss = ss+nodeName;
pNode = m_pXmlDoc->selectSingleNode((_bstr_t)ss);
CString strReture;
if(pNode == NULL)
{
strReture = "0";
m_strErrorMsg = "The Node Value is Null.";
return strReture;
}
bstrname=pNode->Gettext();
//节点值
VARIANT varVal;
pNode->get_nodeTypedValue(&varVal);
CString strValue = (char*)(_bstr_t)varVal;
return strValue;
}
CString Cxml::GetChildNodeInfo(char nodeName[],char childNodeName[],int nCnt)//.获得二级子节点的值
{
::CoInitialize(NULL); //初始化COM
MSXML2::IXMLDOMNodePtr pNode = NULL;
MSXML2::IXMLDOMNodeListPtr pNodeList = NULL;
MSXML2::IXMLDOMNodeListPtr pChildList = NULL;
bstr_t bstrname;
CString ss="//"; // "//..."表示在任意一层查找
ss = ss+nodeName;
pNodeList = m_pXmlDoc->selectNodes((_bstr_t)ss);//获得名字为nodeName的所有节点
pNodeList->get_item(nCnt-1,&pNode);//获得第nCnt个节点值
pNode->get_childNodes(&pChildList);//获得第nCnt个节点的子节点列表
long lCnt;
pChildList->get_length(&lCnt); //获得所含子节点个数
bstr_t bstrChild,bstrChildInfo;
CString strChild;
CString strChildInfo;
MSXML2::IXMLDOMNodePtr pChildNode = NULL;
for(int i=0;i<lCnt;i++)
{
pChildNode = pChildList->item[i];//得到第i个子节点
bstrChild = pChildNode->GetbaseName();
bstrChildInfo = pChildNode->Gettext();
strChild = (char*)(_bstr_t)bstrChild;
strChildInfo = (char*)(_bstr_t)bstrChildInfo;
if (strcmp(strChild,childNodeName) == 0)
{
return strChildInfo;
}
}
return "";
}
BOOL Cxml::CreateFile(CString strFileName)
{
::CoInitialize(NULL);//初始化CO
try
{
m_strXmlName = strFileName;
HRESULT hr;
hr = m_pXmlDoc.CreateInstance(__uuidof(MSXML2::DOMDocument));
if (FAILED(hr))
{
m_strErrorMsg = "XML CreateInstance Error!";
return FALSE;
}
MSXML2::IXMLDOMProcessingInstructionPtr pPI=NULL;
pPI=m_pXmlDoc->createProcessingInstruction((_bstr_t)"xml",(_bstr_t)"version=\"1.0\" encoding=\"UTF-8\"");
_variant_t vNullVal;
vNullVal.vt=VT_NULL;
m_pXmlDoc->appendChild(pPI);if (FAILED(hr))
{
m_strErrorMsg = "XML CreateInstance Error!";
return FALSE;
}
}
catch (...)
{
return FALSE;
}
return TRUE;
}
BOOL Cxml::CreateRootNode(CString strNode)
{
HRESULT hr;
hr = m_pXmlDoc->raw_createElement((_bstr_t)strNode, &m_pXmlRoot);//生成xml文件的根节点
if (FAILED(hr))
{
m_strErrorMsg = "XML RootNode Create Error";
return FALSE;
}
m_pXmlDoc->raw_appendChild(m_pXmlRoot, NULL);
return TRUE;
}
BOOL Cxml::InsertNode(CString strChileNode,CString strChileValue)
{
HRESULT hr;
hr = m_pXmlDoc->raw_createElement((_bstr_t)strChileNode, &m_pXmlChildNode);//生成xml文件的节点
if (FAILED(hr))
{
m_strErrorMsg = "XML Node Create Error";
return FALSE;
}
m_pXmlChildNode->Puttext(strChileValue.AllocSysString());
m_pXmlRoot->appendChild(m_pXmlDoc->createTextNode("\n\t")); //换行
m_pXmlRoot->appendChild(m_pXmlChildNode);//增加节点
return TRUE;
}
//第二层为子节点数据
BOOL Cxml::InsertNodeWithChildNode(int nLevel,//第几层为节点数据
int nNode,//子节点数
int nChildNode,//子子节点数
CString strNodeName[],//各层节点名
CString strChildNodeTxt[] //子子节点数据
)
{
HRESULT hr;
hr = m_pXmlDoc->raw_createElement((_bstr_t)strNodeName[0], &m_pXmlChildNode);//生成xml文件的节点
MSXML2::IXMLDOMElementPtr pNode;
MSXML2::IXMLDOMElementPtr pChildNode;
for (int i=0;i<nNode;i++)
{
m_pXmlDoc->raw_createElement((_bstr_t)strNodeName[1], &pNode);//生成xml文件的子节点
for(int j=0; j<nChildNode; j++)
{
m_pXmlDoc->raw_createElement((_bstr_t)strNodeName[nLevel+j], &pChildNode);
pChildNode->Puttext((strChildNodeTxt[nChildNode*i + j]).AllocSysString());
pNode->appendChild(m_pXmlDoc->createTextNode("\n\t\t\t")); //换行
pNode->appendChild(pChildNode);//增加子节点
}
pNode->appendChild(m_pXmlDoc->createTextNode("\n\t\t")); //换行
m_pXmlChildNode->appendChild(m_pXmlDoc->createTextNode("\n\t\t")); //换行
m_pXmlChildNode->appendChild(pNode);//增加节点
}
m_pXmlChildNode->appendChild(m_pXmlDoc->createTextNode("\n\t")); //换行
m_pXmlRoot->appendChild(m_pXmlDoc->createTextNode("\n\t")); //换行
m_pXmlRoot->appendChild(m_pXmlChildNode);//增加节点
return true;
}
BOOL Cxml::InsertChildNode(CString strNode,CString strChildNode,CString strValue)
{
HRESULT hr;
hr = m_pXmlDoc->raw_createElement((_bstr_t)strNode, &m_pXmlChildNode);//生成xml文件的节点
if (FAILED(hr))
{
m_strErrorMsg = "XML Node Create Error";
return FALSE;
}
MSXML2::IXMLDOMElementPtr pNode;
m_pXmlDoc->raw_createElement((_bstr_t)strChildNode, &pNode);//生成m_pXmlChildNode的子节点
pNode->Puttext(strValue.AllocSysString());//写入子节点值
m_pXmlChildNode->appendChild(m_pXmlDoc->createTextNode("\n\t\t")); //换行
m_pXmlChildNode->appendChild(pNode);//增加节点
m_pXmlChildNode->appendChild(m_pXmlDoc->createTextNode("\n\t")); //换行
m_pXmlRoot->appendChild(m_pXmlDoc->createTextNode("\n\t")); //换行
m_pXmlRoot->appendChild(m_pXmlChildNode);//增加节点
return true;
}
BOOL Cxml::InsertChildNodes(CString strNode,CString strChildNode[],CString strValue[],int nChildNodeNum)
{
HRESULT hr;
hr = m_pXmlDoc->raw_createElement((_bstr_t)strNode, &m_pXmlChildNode);//生成xml文件的节点
if (FAILED(hr))
{
m_strErrorMsg = "XML Node Create Error";
return FALSE;
}
MSXML2::IXMLDOMElementPtr pNode;
for (int i=0; i<nChildNodeNum; i++)
{
m_pXmlDoc->raw_createElement((_bstr_t)strChildNode[i], &pNode);//生成m_pXmlChildNode的子节点
pNode->Puttext(strValue[i].AllocSysString());//写入子节点值
m_pXmlChildNode->appendChild(m_pXmlDoc->createTextNode("\n\t\t")); //换行
m_pXmlChildNode->appendChild(pNo
- 1
- 2
前往页