以下图为例:
//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(); //保存文件
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());
原文链接:https://www.f2er.com/xml/297125.html