使用msxml4创建element
代码如下:请高手指教。
IXMLDOMElement* CActionXml::BuildNode(CString& sNodeName,
CString& sAttrName,CString& sAttrValue, CString& sText,
IXMLDOMDocument* pDom)
{
IXMLDOMElement *pe=NULL;
IXMLDOMAttribute *pa=NULL;
IXMLDOMAttribute *pa1=NULL;
BSTR bstr = NULL;
VARIANT var;
HRESULT hr;
VariantInit(&var);
//创建element
//bstr = SysAllocString(L"kjkj");//若使用这一句程序成功;
if(!sNodeName.IsEmpty()){
bstr=sNodeName.AllocSysString();//由于使用该句使下一句fail
hr=pDom->createElement(bstr,&pe);//该句在执行时fail,为什么?
SysFreeString(bstr);
bstr=NULL;
}
//创建elment文本
if(!sText.IsEmpty()){
bstr=sText.AllocSysString();
hr=pe->put_text(bstr);
SysFreeString(bstr);
bstr=NULL;
}
//创建elemnt属性
if(!sAttrName.IsEmpty()){
bstr=sAttrName.AllocSysString();
var=VariantString(sAttrValue.AllocSysString());
hr=pDom->createAttribute(bstr,&pa);
hr=pa->put_value(var);
hr=pe->setAttributeNode(pa,&pa1);
SysFreeString(bstr);
bstr=NULL;
VariantClear(&var);
}
//资源释放
//if(pa) pa->Release();
//if(pa1) pa1->Release();
//if(pe) pe->Release();
return pe;
}
问题点数:50、回复次数:2
Top
1 楼 liuxianzhi(碧渊) 回复于 2004-06-11 12:44:29 得分 20
用这样的方式:
MSXML2::IXMLDOMElement * pTempElement = NULL;
BSTR tempElementName = _bstr_t(strTopElementName);
hrErr = pXMLDOMDoc->createElement(tempElementName,&pTempElement);
SysFreeString(tempElementName);
if (hrErr == S_OK)
//创建顶层元素节点成功
{......}
Top
2 楼 zhp80(zhp80) 回复于 2004-06-14 14:38:43 得分 30
参见
BOOL CSysConfig::SaveDefault()
{
CString XMLString = "";
XML::IXMLDOMElementPtr pXMLNodA = NULL;
XML::IXMLDOMElementPtr pXMLNodB = NULL;
XML::IXMLDOMAttributePtr pXMLAttr = NULL;
XML::IXMLDOMDocument2Ptr pXMLFile = NULL;
pXMLFile.CreateInstance(__uuidof(XML::DOMDocument40));
XMLString = "<?xml version=\"1.0\" encoding=\"GB2312\"
?><TrafficCapture version=\"1.0\"
></TrafficCapture>";
pXMLFile->loadXML(XMLString.AllocSysString());
if (!pXMLFile->parsed) {
pXMLFile = NULL;
return FALSE;
}
XML::IXMLDOMElementPtr pXMLRoot = pXMLFile->documentElement;
pXMLNodA = pXMLFile->createElement("Picture");
pXMLAttr = pXMLFile->createAttribute("Width");
pXMLAttr->value = CComVariant(mImageWidth);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("Height");
pXMLAttr->value = CComVariant(mImageHeight);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("Quality");
pXMLAttr->value = CComVariant(mJPGQuality);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("Path");
pXMLAttr->value = CComVariant(mSavePath);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLRoot->appendChild(pXMLNodA);
pXMLNodA = pXMLFile->createElement("Queue");
pXMLAttr = pXMLFile->createAttribute("Vehicle");
pXMLAttr->value = CComVariant(mMaxVehicleQueue);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("CloseShot");
pXMLAttr->value = CComVariant(mCloseShotImageCount);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("Panorama");
pXMLAttr->value = CComVariant(mPanoramaImageCount);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLRoot->appendChild(pXMLNodA);
pXMLNodA = pXMLFile->createElement("DBMS");
pXMLAttr = pXMLFile->createAttribute("ConnString");
pXMLAttr->value = CComVariant(mConnStr);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("AddCaptureInfo");
pXMLAttr->value = CComVariant(mStoreProcedure);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("BackList");
pXMLAttr->value = CComVariant(mBlackListView);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLRoot->appendChild(pXMLNodA);
pXMLNodA = pXMLFile->createElement("RTHost");
pXMLAttr = pXMLFile->createAttribute("HostName");
pXMLAttr->value = CComVariant(mIP);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("CommuPort");
pXMLAttr->value = CComVariant(mTCP);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLRoot->appendChild(pXMLNodA);
pXMLNodA = pXMLFile->createElement("PanoramaSection");
pXMLAttr = pXMLFile->createAttribute("Length");
pXMLAttr->value = CComVariant(3);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("Video");
pXMLAttr->value = CComVariant(0);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLRoot->appendChild(pXMLNodA);
pXMLNodA = pXMLFile->createElement("Mechanism");
pXMLAttr = pXMLFile->createAttribute("OCR");
pXMLAttr->value = CComVariant(mOCR);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("SaveALL");
pXMLAttr->value = CComVariant(mSaveALL);
pXMLNodA->attributes->setNamedItem(pXMLAttr);
pXMLRoot->appendChild(pXMLNodA);
pXMLNodA = pXMLFile->createElement("Peripheral");
pXMLNodB = pXMLFile->createElement("SoloNC");
pXMLAttr = pXMLFile->createAttribute("ComPort");
pXMLAttr->value = CComVariant(mSoloNCPort);
pXMLNodB->attributes->setNamedItem(pXMLAttr);
pXMLAttr = pXMLFile->createAttribute("PortSetting");
pXMLAttr->value = CComVariant("38400,n,8,1");
pXMLNodB->attributes->setNamedItem(pXMLAttr);
pXMLNodA->appendChild(pXMLNodB);
pXMLRoot->appendChild(pXMLNodA);
XMLString = "TrafficCapture.CFG";
pXMLFile->save(XMLString.AllocSysString());
pXMLFile = NULL;
return TRUE;
}