这是在下想要生成的文件:<?xml version="1.0" encoding="gb2312" ?>
<clientinfo>
<servicetype>china</servicetype>
<connection>
<display>HELLO</display>
</connection>
</clientinfo> MSXML2::IXMLDOMDocumentPtr pDoc;
MSXML2::IXMLDOMElementPtr xmlRoot ;
MSXML2::IXMLDOMElementPtr xmlRoot2 ;
MSXML2::IXMLDOMElementPtr childNode ;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument30));
pDoc->raw_createElement((_bstr_t)(char*)"clientinfo", &xmlRoot);
pDoc->raw_appendChild(xmlRoot, NULL); pDoc->raw_createElement((_bstr_t)(char*)"servicetype", &childNode);
childNode->Puttext("china");//节点值
xmlRoot->appendChild(childNode);pDoc->raw_createElement((_bstr_t)(char*)"servertype", &childNode);
childNode->Puttext("primary");
xmlRoot->appendChild(childNode); 正如你所看到的那样,这些代码只能实现:
<clientinfo>
<servicetype>china</servicetype>
</clientinfo>
我的问题有2个.....
1.如何生成:
<?xml version="1.0" encoding="gb2312" ?>
这个是用什么方法调用出来的那,请问大家〉。2.如何生成子节点?即:
<clientinfo>
<servicetype>china</servicetype>
<connection>
<display>HELLO</display>
</connection>
</clientinfo>
中的 <connection>
</connection>
那?
谢谢!
{
//Log LogonName="[email protected]" FirstSessionID="1" LastSessionID="165"
CString strLogonName(lpszLogonName);
m_strMyLogonName=lpszmyLogonName;
CString strXMLFile=GetChatHistoryFile(strLogonName);
try{
do{
IXMLDOMDocument2Ptr pDoc;
HRESULT hr = pDoc.CreateInstance(__uuidof(MSXML2::FreeThreadedDOMDocument40));
if(pDoc==NULL) break;
MSXML2::IXMLDOMElementPtr pRoot;
if(pDoc){
pDoc->async = false;
//try load first
do{
if(pDoc->load(T2OLE((LPTSTR)(LPCTSTR)strXMLFile))!=VARIANT_TRUE)break;
pRoot=pDoc->documentElement;
if(pRoot==NULL)break;
if(pRoot->getAttributeNode(L"FirstSessionID")){
m_nFirstSessionID=pRoot->getAttribute(L"FirstSessionID");
}
else{
m_nFirstSessionID=0;
}
if(pRoot->getAttributeNode(L"LastSessionID")){
m_nLastSessionID=pRoot->getAttribute(L"LastSessionID");
}
else{
m_nLastSessionID=0;
}
m_nSessionID=m_nLastSessionID+1;
m_pHistory=pDoc;
m_pRoot=pRoot;
m_strLogonName=strLogonName;
return TRUE;//load successful
}while(FALSE);
//load failed,create it
MSXML2::IXMLDOMProcessingInstructionPtr pProcessingInstruction;
pProcessingInstruction=pDoc->createProcessingInstruction(_T("xml"),
_T("version='1.0'"));
pDoc->appendChild(pProcessingInstruction); pProcessingInstruction=pDoc->createProcessingInstruction(_T("xml-stylesheet"),
_T("type='text/xsl' href='..\\MessageLog.xsl'"));
pDoc->appendChild(pProcessingInstruction); pRoot=pDoc->createElement(_T("Log"));
pRoot->setAttribute(L"FirstSessionID",(long)1);
pDoc->appendChild(pRoot);
pRoot->setAttribute(L"LastSessionID",(long)1);
CreateDirectory(theApp.AppendAppPath(_T("Log")),NULL);
CString strSubDir;
strSubDir.Format(_T("Log\\%s"),m_strMyLogonName);
CreateDirectory(theApp.AppendAppPath(strSubDir),NULL);
pDoc->save(T2OLE((LPTSTR)(LPCTSTR)strXMLFile)); m_nFirstSessionID=1;
m_nLastSessionID=1;
m_nSessionID=1;
m_pHistory=pDoc;
m_pRoot=pRoot;
m_strLogonName=strLogonName;
return TRUE;//load successful
}
}while(FALSE);
}
catch(_com_error &e){
dump_com_error(e);
}
catch(CException*e){
e;
}
catch(HRESULT hr){
hr;
}
catch(...){
}
return FALSE;
}
{
IXMLDOMNodePtr pXMLDomNode=pExchange->m_pXMLDomElement->selectSingleNode(lpszChildElement);
pExchange->m_strCurrent=lpszChildElement;
if(pExchange->m_bLoad){
rChildElementText=(LPCTSTR)pXMLDomNode->text;
}
else{
if(pXMLDomNode==NULL){
pXMLDomNode=pExchange->m_pXMLDoc->createElement(lpszChildElement);
pExchange->m_pXMLDomElement->appendChild(pXMLDomNode);
}
if(pXMLDomNode){
pXMLDomNode->text=(LPCTSTR)rChildElementText;
}
}}
createElement
appendChild
save
再用appendChild把创建的节点对像附到你的xml中去
最后用save保存就OK了其实你也挺懒的,老大的代码里什么都有,还把关键的函数找出来你还不满意,呵呵
这个例子很全的,可以解决你的问题