VC6.0下创建xml文件有很多方式,尝试了timyxml,但是他不支持文件声明等的写入,因此转战MSXML
MSXML有很多版本,这里用的是msxml3
首先在头文件中引入
#import "msxml3.dll"
using namespace MSXML2;
其次编写创建xml文件方法
void CCVideoDlg::CreateXML()
{
MSXML2::IXMLDOMDocument2Ptr pXMLDom;
MSXML2::IXMLDOMProcessingInstructionPtr pi;
HRESULT hr;
CoInitialize(NULL);
hr = pXMLDom.CreateInstance(__uuidof(DOMDocument));
if (FAILED(hr))
{
printf("Failed to CreateInstance on an XML DOM");
}
pXMLDom->preserveWhiteSpace = VARIANT_TRUE;
//添加声明
pi = pXMLDom->createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
if (pi != NULL) {
pXMLDom->appendChild(pi);
pi.Release();
}
//创建样式表
pi = pXMLDom->createProcessingInstruction("xml-stylesheet","type='text/xsl' href='./../utils/detail.xsl'");
if (pi != NULL) {
pXMLDom->appendChild(pi);
pi.Release();
}
// 创建注释
MSXML2::IXMLDOMCommentPtr pc;
pc = pXMLDom->createComment("xmlReport.xml created using XML DOM object.");
if (pc != NULL) {
pXMLDom->appendChild(pc);
pc.Release();
}
char CurrentDateTime[32];
getCurrentTime(CurrentDateTime);
// 创建root节点
MSXML2::IXMLDOMElementPtr pb,pd;
pb= pXMLDom->createElement("body");
pXMLDom->appendChild(pb);
pd = pXMLDom->createElement("Details");
MSXML2::IXMLDOMAttributePtr pa;
pa = pXMLDom->createAttribute("time");
if (pa != NULL)
{
pa->value = CurrentDateTime;
pd->setAttributeNode(pa);
}
pa = pXMLDom->createAttribute("threshold");
if (pa != NULL)
{
CString threadThold;
threadThold.Format("%d",m_sliderCtrl.GetPos());
pa->value = (_bstr_t)threadThold;
pd->setAttributeNode(pa);
}
pa = pXMLDom->createAttribute("diffPercent");
if (pa != NULL)
{
CString diff;
GetDlgItemText(IDC_diffRateEDIT1,diff);
pa->value =(_bstr_t)diff;
pd->setAttributeNode(pa);
}
pb->appendChild(pd);
pb->appendChild(pXMLDom->createTextNode("\n\t"));
//释放节点
pd.Release();
pb.Release();
hr = pXMLDom->save((_variant_t)saveXMLReportName);
if (FAILED(hr))
{
printf("Failed to save DOM to xmlReport.xml\n");
}
else
{
printf("DOM saved to xmlReport.xml\n");
}
if (pXMLDom)
pXMLDom.Release();
CoUninitialize();
}
void CCVideoDlg::AddXMLNode(CString filePath,CString snapTimes,char currentTime[32],CString cameraId,int countNum,CString state){MSXML2::IXMLDOMDocument2Ptr pDoc;HRESULT hr = pDoc.CreateInstance( __uuidof(MSXML2::DOMDocument30), NULL, CLSCTX_INPROC_SERVER); if (FAILED(hr)) { printf("Failed to CreateInstance on an XML DOM"); }pDoc->async = VARIANT_FALSE;pDoc->validateOnParse = VARIANT_FALSE;pDoc->resolveExternals = VARIANT_FALSE;if( pDoc->load((_variant_t)filePath) != VARIANT_TRUE){printf("Fail reason: %s\n", (LPCSTR)pDoc->parseError->Getreason());}else{MSXML2::IXMLDOMNodePtr pNode = pDoc->selectSingleNode(L"body/RESULTS");//首先要找到RESULTS节点,因为是要在它之后插入pNode->appendChild(pDoc->createTextNode("\n\t"));MSXML2::IXMLDOMElementPtr pResultNode=pDoc->createNode(CComVariant(NODE_ELEMENT),"RESULT","");pResultNode->appendChild(pDoc->createTextNode("\n\t"));pNode->appendChild(pResultNode);pNode.Release();pResultNode->setAttribute("snapTimes",(_bstr_t)snapTimes);pResultNode->setAttribute("currentTime",currentTime);pResultNode->setAttribute("TV_Name",(_bstr_t)cameraId);CString strNums;strNums.Format("%d",countNum);pResultNode->setAttribute("diffPercent",(_bstr_t)strNums);pResultNode->setAttribute("state",(_bstr_t)state);//图片路径节点CString imgSrc;imgSrc.Format("%s_%s.jpeg",currentTime,snapTimes);pResultNode->setAttribute("imgSrc",(_bstr_t)imgSrc);pDoc->save((_variant_t)filePath);pResultNode.Release(); }if (pDoc) pDoc.Release();}