XML 在VC下的运用

XXXNNXXX 2009-12-06 05:20:57
load一个XML文件后 请问会吧整个文件都加载到内存中吗

保存的时候是覆盖源文件的形式保存吗 ??

有不覆盖的形式保存的吗??
...全文
369 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
遇到大好人了
「已注销」 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 xxxnnxxx 的回复:]
楼上的请问能给份 加密接口文档的借口吗
[/Quote]
加密接口是用于SQLite3的。
如果你需要,可以下载wxSQLite3,里面有代码,C代码,加入后,编译即可支持加密。
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
哦看错了 这个似乎没有涉及到加密
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
谢谢 楼上的
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
楼上的请问能给份 加密接口文档的借口吗
「已注销」 2009-12-06
  • 打赏
  • 举报
回复
你只需要把解压后的tinyxml中:
tinystr.h
tinystr.cpp
tinyxml.h
tinyxml.cpp
tinyxmlerror.cpp
tinyxmlparser.cpp
加入你的工程即可。
当然,你可以编译成静态库,然后只包含其头文件。

「已注销」 2009-12-06
  • 打赏
  • 举报
回复
这是我在一个工程里使用的tinyxml封装:
/*
** Copyright (C) QPSOFT.COM All rights reserved.
*/

#ifndef SDK_CONFIG_H_
#define SDK_CONFIG_H_

#include "xml/tinyxml.h"

// ----------------------------------------------------------------------------
// 配置文件保存在xml文件中,每个子功能对应一个配置文件
// ----------------------------------------------------------------------------
class Config
{
public:
SDKEXPORT Config(const char* fileName = "config");
SDKEXPORT ~Config();

public:
// 获取整型值,当查询值不存在时,保存默认值
SDKEXPORT int getInt(const char* element, const int def = 0);

// 设置整型值
SDKEXPORT bool setInt(const char* element, const int val);

// 获取布尔值
SDKEXPORT bool getBool(const char* element, const bool def = false);

// 设置布尔值
SDKEXPORT bool setBool(const char* element, const bool val);

// 获取文本
SDKEXPORT shared_ptr<wstring> getText(const char* element, const wchar_t* def = L"", const bool cdata = false);

// 设置文本
SDKEXPORT bool setText(const char* element, const wchar_t* val, const bool cdata = false);

private:
// 如果载入失败,则说明文件不存在或不规范,需要重建配置文件
SDKEXPORT bool loadCfgFile();

// 重新加载
SDKEXPORT bool reLoad();

// 创建配置文件
SDKEXPORT bool createCfgFile();

private:
// 获取根结点
TiXmlElement* getRoot();

private:
static const string _cfgFilePath;
const string _cfgFile;

TiXmlDocument _doc;
TiXmlElement* _root;
};

#endif // SDK_CONFIG_H_

/*
** Copyright (C) QPSOFT.COM All rights reserved.
*/

#include "config.h"

const string Config::_cfgFilePath = *WC2MB((*getDataPath()).c_str());

Config::Config(const char* fileName) : _cfgFile(_cfgFilePath + "\\" + fileName + ".xml"),
_doc(_cfgFile.c_str()), _root(NULL)
{
if (!loadCfgFile())
{
if (createCfgFile()) reLoad();
}
}

Config::~Config()
{
_doc.SaveFile();
}

bool Config::loadCfgFile()
{
return _doc.LoadFile();
}

bool Config::reLoad()
{
return loadCfgFile();
}

bool Config::createCfgFile()
{
TiXmlDocument doc(_cfgFile.c_str());
TiXmlDeclaration* dec = new TiXmlDeclaration("1.0", "utf-8", "");
doc.LinkEndChild(dec);

TiXmlComment* cmt = new TiXmlComment((*WC2MB(DEF_AppName)).c_str());
doc.LinkEndChild(cmt);

TiXmlElement* root = new TiXmlElement("Config");
doc.LinkEndChild(root);

return doc.SaveFile();
}

TiXmlElement* Config::getRoot()
{
if (_root == NULL) _root = _doc.RootElement();
ASSERT(_root);
return _root;
}

int Config::getInt(const char* element, const int def)
{
TiXmlElement* target = getRoot()->FirstChildElement(element);

if (target != NULL)
{
int val;
int ret = target->QueryIntAttribute("int", &val);
if (ret == TIXML_SUCCESS) return val;
else if (ret == TIXML_NO_ATTRIBUTE) target->SetAttribute("int", def);
}
else
{
TiXmlElement* add = new TiXmlElement(element);
add->SetAttribute("int", def);
getRoot()->LinkEndChild(add);
}

return def;
}

bool Config::setInt(const char* element, const int val)
{
TiXmlElement* target = getRoot()->FirstChildElement(element);

if (target != NULL)
{
target->SetAttribute("int", val);
return true;
}

return false;
}

bool Config::getBool(const char* element, const bool def)
{
int i = def ? 1 : 0;
TiXmlElement* target = getRoot()->FirstChildElement(element);

if (target != NULL)
{
int val;
int ret = target->QueryIntAttribute("bool", &val);
if (ret == TIXML_SUCCESS) return val ? true : false;
else if (ret == TIXML_NO_ATTRIBUTE) target->SetAttribute("bool", i);
}
else
{
TiXmlElement* add = new TiXmlElement(element);
add->SetAttribute("bool", i);
getRoot()->LinkEndChild(add);
}

return def;
}

bool Config::setBool(const char* element, const bool val)
{
TiXmlElement* target = getRoot()->FirstChildElement(element);

if (target != NULL)
{
target->SetAttribute("bool", val);
return true;
}

return false;
}

shared_ptr<wstring> Config::getText(const char* element, const wchar_t* def, const bool cdata)
{
TiXmlElement* target = getRoot()->FirstChildElement(element);

if (target != NULL)
{
return UT2WC(target->GetText());
}
else
{
TiXmlElement* add = new TiXmlElement(element);
TiXmlText* txt = new TiXmlText((*WC2UT(def)).c_str());
txt->SetCDATA(cdata);
add->LinkEndChild(txt);
getRoot()->LinkEndChild(add);
}

return shared_ptr<wstring>(new wstring(def));
}

bool Config::setText(const char* element, const wchar_t* val, const bool cdata)
{
TiXmlElement* target = getRoot()->FirstChildElement(element);

if (target != NULL)
{
target->Clear();
TiXmlText* txt = new TiXmlText((*WC2UT(val)).c_str());
txt->SetCDATA(cdata);
target->LinkEndChild(txt);
return true;
}

return false;
}
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
好的谢谢 我下载tinyxml这个来试试

「已注销」 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 xxxnnxxx 的回复:]
谢谢 楼上的了
我打开就只是保存1M左右的字符串
请问那个tinyxml为何  什么库是tinyxml模式的  msxml是什么模式的??
我的设想就是 所有的数据都在内存 最后的一部才保存  不是增加一条数据就保持一条
似乎平常的保持模式都是覆盖形式的 频繁的保持数据到硬盘的话 我担心占了硬盘的流量
[/Quote]
用tinyxml吧,才1MB的数据而已。
其他XML我不太清楚,因为不知道其机理。
但tinyxml我用过好几年了,采取的是读到内存,在内存中操作的方式。

如果数据再大点,考虑用SQLite3,不错的数据库,免费开源高效,有加密接口(wxSQLite3中有接口源码)。
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
谢谢 楼上的了
我打开就只是保存1M左右的字符串
请问那个tinyxml为何 什么库是tinyxml模式的 msxml是什么模式的??
我的设想就是 所有的数据都在内存 最后的一部才保存 不是增加一条数据就保持一条
似乎平常的保持模式都是覆盖形式的 频繁的保持数据到硬盘的话 我担心占了硬盘的流量
「已注销」 2009-12-06
  • 打赏
  • 举报
回复
一次性读入内存的方法,修改都在内存中进行,避免了大量的IO浪费。
这是小型XML解析器通用的作法。
其中,以tinyxml为典型:真是太好用了!!
「已注销」 2009-12-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xxxnnxxx 的回复:]
楼上的能详细解释一下吗

我才接触这个玩意儿 也不太了解这个XML得原理

我担心这个如果把整个文件都读入内存的话没加一条数据就调用save的话很浪费硬盘
[/Quote]
既然你才接触这个玩意,就不必担心浪费硬盘的问题。
一个xml能有多大?
不就是用来保存一些数据吗?

大的数据是不用xml的,要用数据库,比如:SQLite3
甚至更大的数据库:MySQL、SQLServer等
XXXNNXXX 2009-12-06
  • 打赏
  • 举报
回复
楼上的能详细解释一下吗

我才接触这个玩意儿 也不太了解这个XML得原理

我担心这个如果把整个文件都读入内存的话没加一条数据就调用save的话很浪费硬盘
「已注销」 2009-12-06
  • 打赏
  • 举报
回复
看XML实现了。
有读到内存的:tinyxml
也有基于事件的:MSXML

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧