这是在下想要生成的文件:<?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>
那?
谢谢!

解决方案 »

  1.   

    BOOL CChatHistory::Create(LPCTSTR lpszmyLogonName,LPCTSTR lpszLogonName)
    {
    //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;
    }
      

  2.   

    void ExchangeChildElementText(CXMLExchange* pExchange,LPCTSTR lpszChildElement,CString& rChildElementText)
    {

    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;
    }
    }}
      

  3.   

    createProcessingInstruction
    createElement
    appendChild
    save
      

  4.   

    呵呵,老大比较懒得废话,我来帮忙解释,:)createProcessingInstruction用这个可以生成你要的<?xml version="1.0" encoding="gb2312" ?>用createElement来创建节点对象
    再用appendChild把创建的节点对像附到你的xml中去
    最后用save保存就OK了其实你也挺懒的,老大的代码里什么都有,还把关键的函数找出来你还不满意,呵呵
      

  5.   

    好好看一下xml SDK中的dynamDOMsmart.cpp
    这个例子很全的,可以解决你的问题
      

  6.   

    这样的回复咋这么多?斑竹的fans真多......