加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

tinyxml操作实例

发布时间:2020-12-16 09:15:40 所属栏目:百科 来源:网络整理
导读:以下图为例: //1.建立元素 TiXmlDocument* pdoc = new TiXmlDocument("p_sys.xml") ; //新建xml文档 TiXmlDeclaration* decl = new TiXmlDeclaration("1.0","gb2312","yes"); pdoc-LinkEndChild(decl); TiXmlElement *p_sys = new TiXmlElement("p_sys"); p

以下图为例:

//1.建立元素

TiXmlDocument* pdoc = new TiXmlDocument("p_sys.xml"); //新建xml文档

TiXmlDeclaration* decl = new TiXmlDeclaration("1.0","gb2312","yes");
pdoc->LinkEndChild(decl);
TiXmlElement *p_sys = new TiXmlElement("p_sys");
pdoc->LinkEndChild(p_sys); //p_doc下添加p_sys元素
TiXmlElement* pp_streamelm = new TiXmlElement("pp_stream")
p_sys->LinkEndChild(pp_streamelm); //p_sys下添加pp_steream元素

p_syselm->SetAttribute("i_dts_start",(const char*)PString(this->i_dts_start));

..........................
TiXmlElement* p_streamelm;
p_streamelm = new TiXmlElement("p_stream");
pp_streamelm->LinkEndChild(p_streamelm); //............................

p_streamelm->SetAttribute("i_track_id",p_stream->i_track_id);
p_streamelm->SetAttribute("i_entry_count",p_stream->i_entry_count); //该函数为重载函数支持const char* 和unsigned int
p_streamelm->SetAttribute("i_dts_start",(const char*)PString(p_stream->i_dts_start));

pdoc->SaveFile(); //保存文件

2.给元素添加属性、赋值以及添加新的子元素

TiXmlElement *p_syselm = pdoc->RootElement();
TiXmlElement *pp_streamelm = p_syselm->FirstChildElement("pp_stream");
TiXmlElement *p_streamelm = pp_streamelm->FirstChildElement("p_stream");

TiXmlElement *entryelm = new TiXmlElement("entry");
pp_streamelm->LinkEndChild(entryelm);
entryelm->SetAttribute("p_stream_id",i_stream);
TiXmlElement *p_i_poselm = new TiXmlElement("i_pos");
pNode = entryelm->InsertEndChild(*p_i_poselm);
pTempChildeElm = pNode->ToElement();
pText = new TiXmlText((const char*)PString(this->i_pos));
pTempChildeElm->InsertEndChild(*pText);

.......................................


3.读取xml文件

TiXmlElement *p_syselm = xml.RootElement(); TiXmlElement *pp_streamelm = p_syselm->FirstChildElement("pp_stream"); p_sys->i_dts_start = strTonum(p_syselm->Attribute("i_dts_start")); p_syselm->QueryUnsignedAttribute("i_pos",&i_tmpdata); p_sys->i_pos = (int64_t)i_tmpdata; TiXmlElement *p_streamelm = pp_streamelm->FirstChildElement("p_stream"); mp4_stream_t* p_stream = m_p_tmpstream[0]; p_streamelm->Attribute("i_track_id",&p_stream->i_track_id); //该函数重载,见下文 p_streamelm->QueryUnsignedAttribute("i_entry_count",&p_stream->i_entry_count); //与Attribute(const char*,int*)的区别是第二个参数类型,int与unsigned int p_stream->i_duration = atoi(p_streamelm->Attribute("i_duration")); p_stream->i_dts_start = strTonum(p_streamelm->Attribute("i_dts_start")); //返回字符串,用自定义函数转换成整数 mp4_entry_t* tmpentry;//获取entry的内容 TiXmlElement* entryelm = pp_streamelm->FirstChildElement("entry"); TiXmlElement* i_poselm = entryelm->FirstChildElement(); tmpentry->i_pos = atoi(i_poselm->FirstChild()->Value()); TiXmlElement* i_sizeelm = i_poselm->NextSiblingElement(); tmpentry->i_size = atoi(i_sizeelm->FirstChild()->Value()); TiXmlElement* i_lengthelm = i_sizeelm->NextSiblingElement(); tmpentry->i_length = atoi(i_lengthelm->FirstChild()->Value()); TiXmlElement* i_flagselm = i_lengthelm->NextSiblingElement(); tmpentry->i_flags = atoi(i_flagselm->FirstChild()->Value());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读