运行环境:VC2005,
系统: windows mobile
我不定时会收到消息,每次收到消息后,便将此条消息写入xml文件中。
xml文件只允许存比如30条消息数,满了后再来消息就把最久一条删除再存储。主要是写入xml文件的方法。 
谢谢!<item>第一条记录</item>
<item>第二条记录</item>
<item>第三条记录</item>
<item>第四条记录</item>

解决方案 »

  1.   

    你用的是什么库在操作XML?msxml?
      

  2.   

    什么库?  我只是想把一条条的记录写入文件中。 这跟库关系?
    好像是tinyxml  第三方开源的吧
      

  3.   

    当然和库有关,如果实现了XML库实现了DOM那基本上每个库的代码是一样的,像tinyxml这类的微型库,没有实现DOM,必须看其本身的节点操作方法。
      

  4.   

    哦。 也就是说,有点麻烦啊。
    哎,郁闷。 要不我直接用::writefile(…………)?
    这样子写进去可以吗? 
      

  5.   

    请问我如果用msxml的话,应该加载哪些东西,什么库?
      

  6.   

    不知道你和我用的是不是一样!
    给你段代码参考一下!void CSecProToolDlg::DBToXML(CString Path)//从数据库读出表数据生成xml
    {
    //CString str="E:\\workarea\\zhanghongtao\\SecProTool\\a.xml"; if(m_LinkStatus==FALSE)
    {
    MessageBox("数据库没有连接!");
    return;
    }
    TiXmlDocument xmlDoc(Path);
    //xmlDoc.LoadFile(str);
    if(!CheckFile(Path))
    {
    TiXmlElement * xmlElement1;
    TiXmlElement * xmlNodeSoftType;
    TiXmlElement * xmlElement2;
    TiXmlElement * xmlNodeSoft;
    TiXmlElement * xmlElement3;
    TiXmlElement * xmlNodeProcess; CMysqlClient client;
    CMysqlStore store1,store2,store3;
    store1.SetTransAction(client.GetConnect());
    store2.SetTransAction(client.GetConnect());
    store3.SetTransAction(client.GetConnect());

    std::string sql_sel1 = "select * from SECURITY_SOFT_TYPE_INFO ";
    store1.Query(sql_sel1);
    int typerowcount=store1.RowCount();
    if(typerowcount==0)
    {
    MessageBox("该数据库为空!");
    return;
    }

    for(int i=0;i<typerowcount;i++)
    {
    xmlElement1=NULL;
    xmlNodeSoftType=NULL;

    xmlNodeSoftType=new TiXmlElement("SOFTTYPE");
    xmlElement1=xmlNodeSoftType->ToElement(); CString SoftTypeName=store1.GetItemValue(i,"type_name").c_str();
    std::string SoftType=store1.GetItemValue(i,"type_id");
    std::string TypeSecRight=store1.GetItemValue(i,"sec_right");
    std::string TypeRe=store1.GetItemString(i,"re"); CEncryptLib Encrypt;
    const char *instr=SoftTypeName.GetBuffer();
    const char *key="123123123123123123123";
    string outstr=Encrypt.EncryptString(instr,key); xmlElement1->SetAttribute("typeid",SoftType.c_str());
    xmlElement1->SetAttribute("typename",outstr.c_str());
    xmlElement1->SetAttribute("sec_right",TypeSecRight.c_str());
    xmlElement1->SetAttribute("re",TypeRe.c_str());
    CString strsql2;
    strsql2.Format("select * from SECURITY_SOFT_INFO where type_id='%s'",SoftType.c_str());
    std::string sql_sel2 =strsql2.GetBuffer() ;

    store2.Query(sql_sel2);
    int softrowcount=store2.RowCount();
    for(int j=0;j<softrowcount;j++)
    {
    xmlElement2=NULL;
    xmlNodeSoft=NULL; xmlNodeSoft=new TiXmlElement("SecSoft");
    xmlElement2=xmlNodeSoft->ToElement(); CString SoftName=store2.GetItemValue(j,"soft_name").c_str();
    std::string SoftID=store2.GetItemValue(j,"soft_id");
    std::string SoftSecRight=store2.GetItemValue(j,"sec_right");
    std::string SoftRe=store2.GetItemString(j,"re"); instr=SoftName.GetBuffer();
    outstr=Encrypt.EncryptString(instr,key); xmlElement2->SetAttribute("softid",SoftID.c_str());
    xmlElement2->SetAttribute("softname",outstr.c_str());
    xmlElement2->SetAttribute("sec_right",SoftSecRight.c_str());
    xmlElement2->SetAttribute("re",SoftRe.c_str()); CString strsql3;
    strsql3.Format("select * from SECURITY_PRO_INFO where soft_id='%s'",SoftID.c_str());
    std::string sql_sel3 =strsql3.GetBuffer();
    store3.Query(sql_sel3);
    int prorowcount=store3.RowCount();
    for(int k=0;k<prorowcount;k++)
    {
    xmlElement3=NULL;
    xmlNodeProcess=NULL;

    xmlNodeProcess=new TiXmlElement("SecProcess");
    xmlElement3=xmlNodeProcess->ToElement(); CString ProcessName=store3.GetItemValue(k,"pro_name").c_str();
    std::string ProcessID=store3.GetItemValue(k,"pro_id").c_str();
    std::string ProcessSecRight=store3.GetItemValue(k,"sec_right").c_str();
    std::string ProcessRe=store3.GetItemString(k,"re").c_str(); instr=ProcessName.GetBuffer();
    outstr=Encrypt.EncryptString(instr,key); xmlElement3->SetAttribute("processid",ProcessID.c_str());
    xmlElement3->SetAttribute("processname",outstr.c_str());
    xmlElement3->SetAttribute("sec_right",ProcessSecRight.c_str());
    xmlElement3->SetAttribute("re",ProcessRe.c_str());

    xmlNodeProcess=xmlElement3;
    xmlNodeSoft->InsertEndChild(*xmlNodeProcess); }
    xmlNodeSoftType->InsertEndChild(*xmlNodeSoft);
    }
    xmlDoc.InsertEndChild(*xmlNodeSoftType);
    }
    xmlDoc.SaveFile();
    delete xmlNodeSoftType;
    CString message;
    message.Format("文件写到'%s'成功!",Path.GetBuffer());
    MessageBox(message);
    }
    else
    {
    MessageBox("该文件已经存在!");
    return;
    }}
      

  7.   

    非常感谢, 你的TiXmlElement 这个东西需要在头文件里加些什么呢。
    或者是需要加什么库?
      

  8.   

    直接写文本文件吧
    与XML可以没关系,LZ就认为他是*.xml的文本文件就行了
    这么简单的问题还要动用什么库,真是^_^
      

  9.   

    我现在也正在研究中,还不太会用C++来操作XML
      

  10.   


    你用的不是tinyxml.h?直接在使用的文件头包含就行了哦!
    要不你把你的tinyxml.h 贴出来一部分,我看看?