写入数据时如何保存为xml
读入数据,然后写入文件,想保存微微xml格式的;
如果用boost的序列化复杂吗?
还是用其他的库方便点。
比如写入book(string),bookname(string),bookquantity(int)这三个变量;
如何进行,表示木有搞过,球教;
[解决办法]
微软的xml语言解析器msxml较为庞大,用起来不是很方便。
推荐使用tinyxml库完成xml文件的读写。官网:http://sourceforge.net/projects/tinyxml/
在此给出一简单的代码片段以供参考:
- C/C++ code
///////////////////////////////////////////////// 保存树所有节点信息(文本、选中状态)到指定xml文件.// lpszFileName : 保存树形控件节点信息的xml文件的名称void CTreeCtrlDlg::WriteToXml2(LPCTSTR lpszFileName){ // 创建xml文件 TiXmlDocument *pDoc = new TiXmlDocument(lpszFileName); //声明xml属性并写入基本的xml头结构 TiXmlDeclaration decl(_T("1.0"), _T("gb2312"), _T("yes")); pDoc->LinkEndChild(&decl); TiXmlNode *pNode = NULL; TiXmlElement *pRootElem = NULL; TiXmlElement *pItemElem = NULL; TiXmlText *pText = NULL; // ---------获取根节点选中状态及文本内容 HTREEITEM hRoot = m_tree_ctrl.GetRootItem(); if (!hRoot) return; LPTSTR lpszFlag = m_tree_ctrl.GetCheck(hRoot) ? _T("true") : _T("false"); CString str = m_tree_ctrl.GetItemText(hRoot); // 插入头结点信息到文档对象pDoc pRootElem = new TiXmlElement(_T("Item")); pRootElem->SetAttribute(_T("text"), str.GetBuffer(str.GetLength())); pRootElem->SetAttribute(_T("checked"), lpszFlag); pNode = pDoc->InsertEndChild(*pRootElem); // 遍历根节点下所有的节点并保存其节点信息 SaveAllItems(hRoot, pNode); // 保存xml BOOL bSave = pDoc->SaveFile(); LPTSTR msg = bSave ? _T("文件已成功保存") : _T("oops, 出现错误..."); MessageBox(msg);}